Just a small insight that might help you make your complex regular expression much simpler..
feel free to later apply any of the tips in answers above..
var text = ".................."; //assuming staring point
........
text = text
.replace(/\r/g,"##R##")
.replace(/\n/g,"##N##")
.replace(/\/\*(.*)\*\//g,"")
.replace(/##R##/g,"\r")
.replace(/##N##/g,"\n")
applying a little (independent) replacement of \r
and \n
will simplify your regex A LOT!,
originally even with g
and m
modifiers (global and "greedy" flags), you still won't succeed removing the comments (unless you custom-build a "character-walker" loop, or run the same reg-ex multiple times...) this is due some characteristics of the regular-expression matching left in limbo since ECMAScript 4 (ECMA-262)
What smart thing are doing here that is worth mentioning ?
This way we apply a nifty little trick known in Discrete mathematics(languages and grammar) as "replacement outside of our grammar", I'm using this unconventionally to "protect" the \r
and \n
areas in the text without actually applying too much computational-power to process them (as in cut/assemble etc..)
Here it's kind of a gamble since, essentially, ##R##
and ##N##
(although not so common), might be an existing phrase, but this is not an issue since the replacement can be infinitesimally-more complex.
In short,
The regular-expressions will be simpler,
The regular-replacements will work as intended without that whitespace-bug..
And \n
and \r
will be restored to their original placement, intact.