Regular expression to remove comment

后端 未结 5 2004
轮回少年
轮回少年 2021-02-07 14:49

I am trying to write a regular expression which finds all the comments in text. For example all between /* */. Example:

/* Hello */

5条回答
  •  不思量自难忘°
    2021-02-07 14:54

    Just an additionnal note about using regex to remove comments inside a programming language file.

    Warning!

    Doing this you must not forget the case where you have the string /* or */ inside a string in the code - like var string = "/*"; - (we never know if you parse a huge code that is not yours)!

    So the best is to parse the document with a programming language and have a boolean to save the state of an open string (and ignore any match inside open string).

    Again a string delimited by " can contain a \" so pay attention with the regex!

提交回复
热议问题