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
, , , ( , ), .