如何匹配所有><除了粗体标签
How to match all >< except bold tag
我有一个正则表达式 [&<>]
用于匹配文本中的 <
和 >
。
喜欢 <i>text</i>
- 将匹配 <, >, <, >,
但我不希望它匹配 <b>
和 </b>
我该怎么做?
示例:<i>match me</i> <b>don't match me</b> <i>match me</i>
将仅匹配 <
和 >
的斜体标签
您可以使用否定环视来实现此目的:
(?<!b)>|(?!<b)(?!</b)<
解释:
(?<!b)>|(?!<b)(?!</b)<
| # match either
> # a >,
(?<!b) # not preceeded by a b
< # or a <,
(?!<b) # not preceeded by a <b
(?!</b) # and neither by </b
环视断言通常必须具有固定长度,这就是为什么我们需要两个左尖括号:一个用于 <b>
,一个用于 </b>
.
我有一个正则表达式 [&<>]
用于匹配文本中的 <
和 >
。
喜欢 <i>text</i>
- 将匹配 <, >, <, >,
但我不希望它匹配 <b>
和 </b>
我该怎么做?
示例:<i>match me</i> <b>don't match me</b> <i>match me</i>
将仅匹配 <
和 >
的斜体标签
您可以使用否定环视来实现此目的:
(?<!b)>|(?!<b)(?!</b)<
解释:
(?<!b)>|(?!<b)(?!</b)<
| # match either
> # a >,
(?<!b) # not preceeded by a b
< # or a <,
(?!<b) # not preceeded by a <b
(?!</b) # and neither by </b
环视断言通常必须具有固定长度,这就是为什么我们需要两个左尖括号:一个用于 <b>
,一个用于 </b>
.