Search code examples
javascriptregexgulp

Regex - find all comments with specific string


Can someone help me with this Javascript regex (it will be running in Gulp)? I want something that will find all one-line comments (both Java and HTML) that have the text "CommentText." These are the two formats:

/* CommentText */
<!-- Comment Text -->

Here is the regex I'm trying, but it's only finding the first one (the Java comment):

(\/*|\/<!--)(.|(CommentText)|[\r\n])*?(\*\/|\/-->)

Solution

  • This regex would work:

    (\/\*|<!--)\s?(Comment\s?Text)\s?(\*\/|-->)
    

    With the comment's text located in the third capture group: (.*)

    Example on Regex101