正则表达式:如何匹配给定html标签外部的单词?

时间:2014-01-27 09:58:08

标签: php regex

假设我有一个像这样的HTML标记:

<p>
    <h1>Some header, which I don't want to match</h1>
    Some text - match it.
    <a href="some-file.html">Some link. Don't match neither href nor link text.<a>
    <span>Some word, which needs to be matched</span>
</p>

简而言之,除了给定的html标签(及其属性)之外,我想在整个内容中匹配一些单词。在给定的示例中,我想排除h1和标签。

将'Some'替换为'Test'后的预期结果:

<p>
    <h1>Some header, which I don't want to match</h1>
    Test text - match it.
    <a href="some-file.html">Some link. Don't match neither href nor link text.<a>
    <span>Test word, which needs to be matched</span>
</p>

1 个答案:

答案 0 :(得分:0)

您可以使用:<(a|h1)[^\>]*?>(some)[^\<]*?<\/\1>来匹配包含some的行和html标记之间的行。

检查一行是否不满足此正则表达式,然后用您所需的替换文本替换一些单词(如果有的话)。

<强> Demo

说明:

enter image description here