如何构建包含周围 html 并且仅匹配 3 次的正则表达式替换


How to build a regex replace that includes surrounding html AND only match 3 times

我多次重复以下字符串(大约 10 次)

<br />
<a href="http://www.someurl.com/someimage.jpg" target="_blank">SOME TEXT</a>

现在我想匹配那段代码,并基本上将其完全从我的字符串中删除。 问题是图像 URL 和"SOME TEXT"总是不同的,我只需要对字符串中这个组合的前 3 个实例(包括换行符 br)重复此操作。

使用正则表达式解析 HTML 是个坏主意,但无论如何都想这样做,请使用:

PHP代码:

preg_replace('/<br's*'/>'s*<a href="[^"]*" target="_blank">[^<]*<'/a>/', '', $str, 3);