Re-expression to remove multi-line comments

I am trying to use this regex (JS):

/\/\*(.*)\*\//g 

To replace

 /* sdandsads */ 

nothing.

But it does not work! What for? o_o

+6
javascript regex
source share
2 answers

the point catches everything except the newlines .. ( if the point is false )

so either use dotall (as mentioned in other answers / comments, this is not supported in javascript, but I will leave it here for reference)

 /\/\*(.*)\*\//gs 

or add space characters \s to expressions

 /\/\*((\s|.)*?)\*\//g 

Strike>

Alan mentioned in his comment the bad work from the answer I gave, so use instead instead .. (which translates all spaces and all without spaces, so that's all ..)

 /\/\*([\s\S]*?)\*\//g 
+12
source share

Two problems:

  • There is no dotall modifier in javascript. You will need to use a hack to resolve the coincidence of newlines, for example using [^] .
  • You are using greedy matching. If there are several comments at your entrance, everything that is between them will be eaten.

Decision:

 /\/\*[^]*?\*\//g 

Example:

 > '/*abc\ncde*/qqq/*iop\n\njj*/'.replace(/\/\*[^]*?\*\//g, '') qqq 
+4
source share

All Articles