0

所以我试图创建一个正则表达式来匹配不同类型的 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

这是我第一次在 stackoverflow 上发帖,所以如果我做了一些非常愚蠢的事情,我只能道歉。

4

1 回答 1

0

使用正则表达式来解析 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>"] ​

http://jsfiddle.net/Mk7xf/

于 2012-07-05T12:54:24.007 回答