正则表达式,排除 html 中的文件名

时间:2021-06-22 01:13:07

标签: php html regex

HTML 代码

<div class="thx_thanked_post"><a title="Click to enlarge" target="_blank" data-fancybox="data-2581" data-type="image" href="images/thx/star.png"><img src="images/thx/star.png" alt="Better response on post dqwdqwdqwqdwdqwqwdwdwd" class="thx_thanked_post_img"></a>Just a "quick" overview of what's good and why.<br>

<a title="Click to enlarge" target="_blank" data-fancybox="data-2581" data-type="image" href="images/thx/kek.png"><img src="images/thx/kek.png" alt="Better response on post dqwdqwdqwqdwdqwqwdwdwd" class="thx_thanked_post_img"></a>
<br>
Note: Hyper speed is referenced multiple times here. This is a bug(?) where after reaching speeds over 160 km/h, you continue accelerating like crazy while traveling in a straight line, allowing you to reach speeds of over 500 km/h with a good boost under ideal conditions. Making good use of hyper speed when possible is huge for cutting time, and much of what makes something good is how easily and consistently it works to give hyper speed.<br>

我想排除带有“stars.png”的 <img> 标签,用 <a> 标签括起来。

我的正则表达式:

<a [^>]*\>([\t]|[\n])*<img[^>]*^(?!.*star\.png$)[^>]*\>([\t]|[\n])*<\/a>

它不起作用。这什么都没有。 正确的匹配应该只选取带有“images/thx/kek.png”而不是“images/thx/star.png”的 <a> 的第二个 <img> 标签。

1 个答案:

答案 0 :(得分:1)

<a [^>]*>([\t]|[\n])*<img(?!.*star\.png)[^>]*>([\t]|[\n])*<\/a> 似乎有效。

^$ 匹配行的开头和结尾,它们可能不是您想要匹配的内容,因此我将它们删除。 [^>]* 匹配 img 标签中的所有内容,并在结束 > 之前停止。前瞻断言 (?!.*star\.png) 从该 > 开始匹配而不是 img 标记中的内容,所以我将它移到 [^>]* 前面。

相关问题