说我有这样的文字:
The quick brown fox <a href="http://www.example1.com">jumps over</a> the <a href="http://www.example2.com">lazy</a> dog
如何将所有字符替换为X,除了&lt; a&gt;之间的字符。标签?在上面的例子中,我想得到:
XXXXXXXXXXXXXXXXXXXX<a href="http://www.example1.com">jumps over</a>XXXX<a href="http://www.example2.com">lazy</a>XXXX
答案 0 :(得分:2)
您可以使用此模式查找不在<a>
标记之间的每个字符:
(?:\G|(?<=</a>))(?:[^<]|<(?!a\b))
模式说明:
(?: # open a non capturing group
\G # contiguous to precedent match or the begining of the string
| # OR
(?<=</a>) # preceded by the closing "a" tags
) # close the non capturing group
(?: # open a non capturing group
[^<] # all that is not a <
| # OR
<(?!a\b) # < not followed by "a" (=not a "a" tag)
) # close the non capturing group
注意:换行符将替换为两个X
(一个用于\r
,另一个用于\n
)。如果要避免此行为,可以将模式更改为:
(?:\G|(?<=</a>))(?:\r\n|[^<]|<(?!a\b))