Comprehensive RegExp to Remove JavaScript Comments

I need to reliably remove all JavaScript comments with a single regex.

I searched StackOverflow and other sites, but nobody takes into account alternating quotes, multi-line comments, line comments, regular expressions, etc.

Are there regular expressions that can remove comments from this:

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 */"
]

Here is jsbin or jsfiddle to check it out.

+4
source share
5 answers

I like the problems :)

Here is my working solution:

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

Replace it with $1.

Spell here: http://jsfiddle.net/LucasTrz/DtGq8/6/

, , , , , ...

NB: , .


((["'])(?:\\[\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 regex s (singleline - . )

:

  • , / : /[/]/

... , :

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

(fiddle, regex101):

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

, , , ( , ), .

+3

-, JavaScript. Q & A: JavaScript- JavaScript

, 1 , :

:

/("(?:[^\r\n\\"]|\\.)*"|'(?:[^\r\n\\']|\\.)*'|\/[^*\/]([^\\\/]|\\.)*\/[gm]*)|\/\/[^\r\n]*|\/\*[\s\S]*?\*\//g

:

/
  (                                     # start match group 1
      "(?:[^\r\n\\"]|\\.)*"             #   match a double quoted string
    | '(?:[^\r\n\\']|\\.)*'             #   match a single quoted string
    | \/[^*\/]([^\\\/]|\\.)*\/[gm]*     #   match a regex literal
  )                                     # end match group 1
  | \/\/[^\r\n]*                        # match a single line break
  | \/\*[\s\S]*?\*\/                    # match a multi-line break
/g

$1 ( 1). , , , 1, , .

regexr :

  var test = [
      "// Code",
      '// Code',
      "'// Code",
      '"// Code',




       "Code",
      "Code",
      "/* Code */",
      "/* Code",
      "Code */",
      '/* Code */',
      '/* Code',
      'Code */',
       "Code",
      /Code\/*/,
      "Code */"
  ]

1 , - , . var x = a / b / g; , !

+1

JavaScript, JavaScript, API- , , . , , , JS .

.

JavaScript- JavaScript

0

,

. , ( ), lookbehind , //" .

You can use the regular expression as a tokenizer (you only need to “take care of string literals, regular expression literals and two types of comments”), but I would recommend using a full-blown JavaScript parser, they are freely available.

0
source

test.replace (/ (/ * ([\ s \ S]?) * /) | (//(.)$)/gm, '');

-1
source

Source: https://habr.com/ru/post/1546740/


All Articles