javascript - 全面的 RegExp 以删除 JavaScript 注释

标签 javascript regex

我需要用一个正则表达式可靠地删除所有 JavaScript 注释。

我搜索过 StackOverflow 和其他网站,但没有一个考虑到交替引号、多行注释、字符串中的注释、正则表达式等。

是否有任何正则表达式可以从中删除注释:

var test = [
    "// Code",
    '// Code',
    "'// Code",
    '"// Code',
    //" Comment",
    //' Comment',
    /* Comment */
    // Comment /* Comment
    /* Comment
     Comment // */ "Code",
    "Code",
    "/* Code */",
    "/* Code",
    "Code */",
    '/* Code */',
    '/* Code',
    'Code */',
    /* Comment
    "Comment",
    Comment */ "Code",
    /Code\/*/,
    "Code */"
]

这是一个 jsbinjsfiddle对其进行测试。

最佳答案

我喜欢挑战:)

这是我的工作解决方案:

/((["'])(?:\\[\s\S]|.)*?\2|\/(?![*\/])(?:\\.|\[(?:\\.|.)\]|.)*?\/)|\/\/.*?$|\/\*[\s\S]*?\*\//gm

将其替换为 $1

在这里 fiddle :http://jsfiddle.net/LucasTrz/DtGq8/6/

当然,正如无数次指出的那样,合适的解析器可能会更好,但仍然...

注意:我在正则表达式字符串的 fiddle 中使用了正则表达式文字,过多的转义会毁掉你的大脑。


分割

((["'])(?:\\[\s\S]|.)*?\2|\/(?![*\/])(?:\\.|\[(?:\\.|.)\]|.)*?\/) <-- the part to keep
|\/\/.*?$                                                         <-- line comments
|\/\*[\s\S]*?\*\/                                                 <-- inline comments

保留的部分

(["'])(?:\\[\s\S]|.)*?\2                   <-- strings
\/(?![*\/])(?:\\.|\[(?:\\.|.)\]|.)*?\/     <-- regex literals

字符串

    ["']              match a quote and capture it
    (?:\\[\s\S]|.)*?  match escaped characters or unescpaed characters, don't capture
    \2                match the same type of quote as the one that opened the string

正则表达式

    \/                          match a forward slash
    (?![*\/])                   ... not followed by a * or / (that would start a comment)
    (?:\\.|\[(?:\\.|.)\]|.)*?   match any sequence of escaped/unescaped text, or a regex character class
    \/                          ... until the closing slash

要删除的部分

|\/\/.*?$              <-- line comments
|\/\*[\s\S]*?\*\/      <-- inline comments

行注释

    \/\/         match two forward slashes
    .*?$         then everything until the end of the line

内联评论

    \/\*         match /*
    [\s\S]*?     then as few as possible of anything, see note below
    \*\/         match */

我不得不使用 [\s\S] 而不是 . 因为不幸的是 JavaScript 不支持正则表达式 s 修饰符(单行- 这个允许 . 也匹配换行符)

此正则表达式适用于以下特殊情况:

  • 字符类中包含 / 的正则表达式模式:/[/]/
  • 转义字符串文字中的换行符

最终boss战

只是为了乐趣...这是眼睛流血的硬核版本:

/((["'])(?:\\[\s\S]|.)*?\2|(?:[^\w\s]|^)\s*\/(?![*\/])(?:\\.|\[(?:\\.|.)\]|.)*?\/(?=[gmiy]{0,4}\s*(?![*\/])(?:\W|$)))|\/\/.*?$|\/\*[\s\S]*?\*\//gm

这添加了以下扭曲的边缘情况(fiddleregex101):

Code = /* Comment */ /Code regex/g  ; // Comment
Code = Code / Code /* Comment */ /g  ; // Comment    
Code = /Code regex/g /* Comment */  ; // Comment

这是高度启发式的代码,您可能不应该使用它(甚至不如以前的正则表达式)并且让边缘情况失效。

关于javascript - 全面的 RegExp 以删除 JavaScript 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24518020/

相关文章:

javascript - 为什么 `(object.fun = object.fun)() use ` window` 但 (object.fun)() 使用 `object` 作为 `this` ?

javascript - jQuery .append() 和脚本行为和管理

javascript - 如何通过静态代码分析显示所有JavaScript全局变量?

python - 在 Twill 中使用正则表达式

swift - 在 Swift 中使用正则表达式

c# - asp.net 和 javascript 中的正则表达式语法是否相同?

javascript - Polyfill 支持 Angular 9 自定义元素?

javascript - 如何让 async.each 等待 .save() 完成?

Javascript 仅当长度 > 2 时才将字符串中每个单词的首字母大写

如果模式以 ; 结尾,则 Java 正则表达式匹配字符串或无