首页 > 正则表达式删除注释疑惑

正则表达式删除注释疑惑

看requirejs 源码。使用正则表达式删除代码内部注释的。

var commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;

function commentReplace(match, multi, multiText, singlePrefix) {
    return singlePrefix || '';
}

//如下例子

var func = function(){
/* comment */
console.log('comment') // comment
return ''
}

 func.toString().replace(commentRegExp,commentReplace);

疑惑:

var commentRegExp = /(\/\*([\s\S]*?)\*\/| \/\/(.*)$)/mg;

func.toString().replace(commentRegExp,'')

也可以嘛?

所以问:requirejs 源码里面

commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;


这里的 ([^:]|^) 有什么作用?

自己又跑去去stackoverflow上面问的!

http://stackoverflow.com/ques...

【热门文章】
【热门文章】