如果这种判断index是否为0的技巧和g flag一起用,可以同时处理单行和多行的commen
t。对吧?
function removeLeadingComment(inputStr){
        var nRemoved = 0;
        return inputStr.replace(/(?:\s*\/\/.*\n|\s*\/\*[\s\S]*?\*\/)/g,
                function(str,offset){
                        //alert(str + offset + "-" + nRemoved);
                        if(offset == nRemoved){
                                nRemoved += str.length;
                                return "";
                        } else {
                                return str;
                        }
                        
                });
}
【 在 Goofect (在线1010小时,显示10小时...) 的大作中提到: 】
: var str="\/\/line1\n\/\/line2\ncode\n\/\/line3";
: alert(str.replace(/(^\/\/.*\n)+/m,function(a,b,c){
:         return c==0? "": a;
: ...................
--
FROM 76.126.237.*