-2

说我有这个文本:

The quick brown fox <a href="http://www.example1.com">jumps over</a> the <a href="http://www.example2.com">lazy</a> dog

除了 <a> 标签之间的字符之外,我怎样才能将所有字符替换为 X?在上面的例子中,我想得到:

XXXXXXXXXXXXXXXXXXXX<a href="http://www.example1.com">jumps over</a>XXXX<a href="http://www.example2.com">lazy</a>XXXX
4

1 回答 1

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(一个 for\r和一个 for \n)。如果要避免这种行为,可以将模式更改为:

(?:\G|(?<=</a>))(?:\r\n|[^<]|<(?!a\b))
于 2013-08-16T16:54:21.757 回答