RegEx用于匹配/替换JavaScript注释(多行和内联)

我需要使用JavaScript RegExp对象从JavaScript源中删除所有JavaScript注释。


我需要的是RegExp的模式。


到目前为止,我发现了这一点:


compressed = compressed.replace(/\/\*.+?\*\/|\/\/.*(?=[\n\r])/g, '');

此模式适用于以下情况:


/* I'm a comment */

或用于:


/*

 * I'm a comment aswell

*/

但是似乎不适用于内联:


// I'm an inline comment

我不是RegEx及其模式的专家,所以我需要帮助。


另外,我想有一个RegEx模式,该模式将删除所有这些类似HTML的注释。


<!-- HTML Comment //--> or <!-- HTML Comment -->

还有那些条件HTML注释,可以在各种JavaScript来源中找到这些注释。


谢谢。


慕村225694
浏览 638回答 3
3回答

米琪卡哇伊

我一直在给togethor一个需要做类似事情的表情。成品是:/(?:((["'])(?:(?:\\\\)|\\\2|(?!\\\2)\\|(?!\2).|[\n\r])*\2)|(\/\*(?:(?!\*\/).|[\n\r])*\*\/)|(\/\/[^\n\r]*(?:[\n\r]+|$))|((?:=|:)\s*(?:\/(?:(?:(?!\\*\/).)|\\\\|\\\/|[^\\]\[(?:\\\\|\\\]|[^]])+\])+\/))|((?:\/(?:(?:(?!\\*\/).)|\\\\|\\\/|[^\\]\[(?:\\\\|\\\]|[^]])+\])+\/)[gimy]?\.(?:exec|test|match|search|replace|split)\()|(\.(?:exec|test|match|search|replace|split)\((?:\/(?:(?:(?!\\*\/).)|\\\\|\\\/|[^\\]\[(?:\\\\|\\\]|[^]])+\])+\/))|(<!--(?:(?!-->).)*-->))/g吓人吧?为了分解,第一部分匹配单引号或双引号内的所有内容,这是避免匹配带引号的字符串所必需的((["'])(?:(?:\\\\)|\\\2|(?!\\\2)\\|(?!\2).|[\n\r])*\2)第二部分匹配以/ * * /分隔的多行注释(\/\*(?:(?!\*\/).|[\n\r])*\*\/)第三部分匹配单行注释,该注释从该行的任何地方开始(\/\/[^\n\r]*(?:[\n\r]+|$))第四到第六部分匹配正则表达式文字中的所有内容,这取决于前面的等号或正则表达式调用之前或之后的文字((?:=|:)\s*(?:\/(?:(?:(?!\\*\/).)|\\\\|\\\/|[^\\]\[(?:\\\\|\\\]|[^]])+\])+\/))((?:\/(?:(?:(?!\\*\/).)|\\\\|\\\/|[^\\]\[(?:\\\\|\\\]|[^]])+\])+\/)[gimy]?\.(?:exec|test|match|search|replace|split)\()(\.(?:exec|test|match|search|replace|split)\((?:\/(?:(?:(?!\\*\/).)|\\\\|\\\/|[^\\]\[(?:\\\\|\\\]|[^]])+\])+\/))我最初忘记的第七个删除了html注释(<!--(?:(?!-->).)*-->)我的开发环境为正则表达式发出错误而导致行中断,因此我遇到了问题,因此我使用了以下解决方案var ADW_GLOBALS = new ObjectADW_GLOBALS = {&nbsp; quotations : /((["'])(?:(?:\\\\)|\\\2|(?!\\\2)\\|(?!\2).|[\n\r])*\2)/,&nbsp; multiline_comment : /(\/\*(?:(?!\*\/).|[\n\r])*\*\/)/,&nbsp; single_line_comment : /(\/\/[^\n\r]*[\n\r]+)/,&nbsp; regex_literal : /(?:\/(?:(?:(?!\\*\/).)|\\\\|\\\/|[^\\]\[(?:\\\\|\\\]|[^]])+\])+\/)/,&nbsp; html_comments : /(<!--(?:(?!-->).)*-->)/,&nbsp; regex_of_doom : ''}ADW_GLOBALS.regex_of_doom = new RegExp(&nbsp; '(?:' + ADW_GLOBALS.quotations.source + '|' +&nbsp;&nbsp; ADW_GLOBALS.multiline_comment.source + '|' +&nbsp;&nbsp; ADW_GLOBALS.single_line_comment.source + '|' +&nbsp;&nbsp; '((?:=|:)\\s*' + ADW_GLOBALS.regex_literal.source + ')|(' +&nbsp;&nbsp; ADW_GLOBALS.regex_literal.source + '[gimy]?\\.(?:exec|test|match|search|replace|split)\\(' + ')|(' +&nbsp;&nbsp; '\\.(?:exec|test|match|search|replace|split)\\(' + ADW_GLOBALS.regex_literal.source + ')|' +&nbsp; ADW_GLOBALS.html_comments.source + ')' , 'g');changed_text = code_to_test.replace(ADW_GLOBALS.regex_of_doom, function(match, $1, $2, $3, $4, $5, $6, $7, $8, offset, original){&nbsp; if (typeof $1 != 'undefined') return $1;&nbsp; if (typeof $5 != 'undefined') return $5;&nbsp; if (typeof $6 != 'undefined') return $6;&nbsp; if (typeof $7 != 'undefined') return $7;&nbsp; return '';}这将返回由引号引起来的字符串文本捕获的任何内容以及在正则表达式文字中完整保留的任何内容,但对于所有注释捕获均返回一个空字符串。我知道这太过分了,很难维护,但是到目前为止,它确实对我有用。
打开App,查看更多内容
随时随地看视频慕课网APP