与正则表达式不匹配html标签的文本
问题描述:
所以我试图创建一个正则表达式来匹配不同种类的html标签中的文本。它应该在这两种情况下匹配的粗体文字:与正则表达式不匹配html标签的文本
<div class="username_container">
<div class="popupmenu memberaction">
<a rel="nofollow" class="username offline " href="http://URL/surfergal.html" title="Surfergal is offline"><strong><!-- google_ad_section_start(weight=ignore) -->**Surfergal**<!-- google_ad_section_end --></strong></a>
</div>
<div class="username_container">
<span class="username guest"><b><a>**Advertisement**</a></b></span>
</div>
我有以下正则表达式尝试没有任何结果:
/<div class="username_container">.*?((?<=^|>)[^><]+?(?=<|$)).*?<\/div>/is
这是我第一次在这里发帖的计算器,所以如果我我正在做一件令人难以置信的蠢事,我只能道歉。
答
使用正则表达式来解析HTML是..辛苦。查看您问题的评论中的链接。
有什么打算用这些比赛呢?这里有一个快速的jQuery脚本登录控制台结果:
var a = [];
$('strong, b').each(function(){
a.push($(this).html());
});
console.log(a);
结果:
["<!-- google_ad_section_start(weight=ignore) -->**Surfergal**<!-- google_ad_section_end -->", "<a>**Advertisement**</a>"]
我认为你应该使用一个解析器来处理这个问题。它可以照顾更多的案件。 – nhahtdh 2012-07-05 12:45:45
相关:http://stackoverflow.com/a/1732454/502381 – JJJ 2012-07-05 12:45:55
你见过http://stackoverflow.com/a/1732454/3978吗? – 2012-07-05 12:48:57