问题
I want to match valid HTML comment only using regex. I have seen many questions on stackoverflow but no one answer this. Patterns allowed to match
<!-- some comments 1 -->
<!-- 1.91 -->
<!--
//-->
<!-- -->
But not these IE-only comments
<!--[if IE]> <![endif]-->
<!--[if !IE]><!--> <!--<![endif]-->
<!--[if IE 6]> <![endif]-->
What I think that it can be done by taking help of '[' if it is found as first character after <!-- it returns false.
any way you can give your own solution.
for php only to use preg_replace
回答1:
I really don't recommend you do this using regex, but if you insist, here is a basic regular expression: http://regex101.com/r/wI2kF7
With anchor tags: http://regex101.com/r/tC9xH4
Good luck.
回答2:
Why use a regular expression? Use a DOMXPath query like the following
'//comment()[not(starts-with(., "[if") or . = "<![endif]")]'
See http://codepad.viper-7.com/Yzs5zq for a working example.
来源:https://stackoverflow.com/questions/12033395/regex-to-find-html-comment-but-not-ie-only-comment