本文介绍了RegExp去除HTML评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
寻找正则表达式匹配序列并替换(最好是PHP,但无所谓)来改变它(开始和结束只是需要保留的随机文本)。
IN:
fkdshfks khh fdsfsk
< div class ='codetop'> CODE:AutoIt< / div>
< div class ='geshimain'>
<! - eg1 - >
< div class =autoitstyle =font-family:monospace;>
< span class =kw3> msgbox< / span>
< / div>
<! - gc2 - >
<! - bXNnYm94 - >
<! - egc2 - >
<! - g2 - >
< / div>
<! - eg2 - >
fdsfdskh
到此OUT:
fkdshfks khh fdsfsk
< div class ='codetop'> CODE:AutoIt< / div>
< div class ='geshimain'>
< div class =autoitstyle =font-family:monospace;>
< span class =kw3> msgbox< / span>
< / div>
< / div>
fdsfdskh
谢谢。
解决方案
你只是想删除评论?如何
s /<! - [^>] - > // g
或稍微好一些(由提问者自己提出):
<! - (?*) - >
但请记住,HTML不是常规的,所以使用正则表达式来解析它会导致你进入一个受到伤害的世界,当有人抛出离奇的边缘案例时。
Looking for a regexp sequence of matches and replaces (preferably PHP but doesn't matter) to change this (the start and end is just random text that needs to be preserved).
IN:
fkdshfks khh fdsfsk
<!--g1-->
<div class='codetop'>CODE: AutoIt</div>
<div class='geshimain'>
<!--eg1-->
<div class="autoit" style="font-family:monospace;">
<span class="kw3">msgbox</span>
</div>
<!--gc2-->
<!--bXNnYm94-->
<!--egc2-->
<!--g2-->
</div>
<!--eg2-->
fdsfdskh
to this OUT:
fkdshfks khh fdsfsk
<div class='codetop'>CODE: AutoIt</div>
<div class='geshimain'>
<div class="autoit" style="font-family:monospace;">
<span class="kw3">msgbox</span>
</div>
</div>
fdsfdskh
Thanks.
解决方案
Are you just trying to remove the comments? How about
s/<!--[^>]*-->//g
or the slightly better (suggested by the questioner himself):
<!--(.*?)-->
But remember, HTML is not regular, so using regular expressions to parse it will lead you into a world of hurt when somebody throws bizarre edge cases at it.
这篇关于RegExp去除HTML评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!