Explain regex that finds CSS comments

后端 未结 2 1462
离开以前
离开以前 2021-02-09 09:55

I found this regex code that finds comments in w3.org\'s CSS grammar page.

\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/
         


        
2条回答
  •  温柔的废话
    2021-02-09 10:39

    The reason yours finds only single line comments is that, in typical regular expressions, . matches anything except newlines; whereas the other one uses a negated character class which matches anything but the specified characters, and so can match newlines.

    However, if you were to fix that (there's usually an option for multiline or "as if single line" matching), you would find that it would match from the /* of the first comment to the */ of the last comment; you would have to use a non-greedy quantifier, .*?, to match no more than one comment.

    However, the more complex regular expression you give is even more complex than that. Based on nikc.org's answer, I believe it is to enforce the restriction that “comments may not be nested”; that is, they must not contain /* within them. In other languages which permit comments /* like /* this */ (that is, an internal /* is neither prohibited nor a nested comment), the pattern \/\*.*?\*\/ would be appropriate to match them.

提交回复
热议问题