0

我正在尝试编写一个正则表达式来匹配边界上的单词,因为文本在 html 中,我需要避免在<a>here more words</a>.

我现在的正则表达式是:/\bword\b/u

示例文本:

<p>Example lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur <a href="">porta lorem nec</a> tortor laoreet gravida.</p>

搜索 wordlorem应该只在开头替换,而不是在<a>.

4

2 回答 2

8

你可以使用如下的一些黑暗力量

<a[^>]*>.*?</a\s*>(*SKIP)(*FAIL)|\blorem\b

让我们分解一下:

<a[^>]*>            # match an opening "a" tag
.*?                 # match anything ungreedy until ...
</a\s*>             # match a closing "a" tag
(*SKIP)(*FAIL)      # skip it
|                   # or
\blorem\b           # match lorem with boundaries

所以基本上我们首先跳过所有a标签,然后我们匹配lorem.

See a working demo

于 2013-11-11T18:13:58.720 回答
1

/u在您的正则表达式中可能不合适或不需要。它通常在 PHP 中表示 unicode,但例如在 JavaScript 中不允许。
或者可能是您正在使用preg_match而不是preg_match_all在您的 PHP

于 2013-11-11T15:53:16.510 回答