Regex to remove multi line comments

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

I am trying to use this regex (JS):

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

To replace

/*
sdandsads
*/

with nothing.

2条回答
  •  猫巷女王i
    2021-01-05 13:40

    Two problems:

    1. In javascript, there's no dotall modifier. You'll have to use a hack to allow matching newlines, such as using [^].
    2. You're using greedy matching. If there are multiple comments in your input, everything between them will be eaten.

    Solution:

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

    Example:

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

提交回复
热议问题