Regex to remove multi line comments

前端 未结 2 601
-上瘾入骨i
-上瘾入骨i 2021-01-05 13:02

I am trying to use this regex (JS):

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

To replace

/*
sdandsads
*/

with nothing.

2条回答
  •  北海茫月
    2021-01-05 13:49

    the dot catches everything except newlines..( if the dotall is false )

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

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

    or add the whitespace chars \s in your expressions

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

    Alan mentioned in his comment a bad performance from the answer i gave so use the following instead.. ( which translates to everything whitespace and everything non whitespace, so everything.. )

    /\/\*([\s\S]*?)\*\//g
    

提交回复
热议问题