Regex to find html comment but not ie only comment

北战南征 提交于 2019-12-06 12:37:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!