简体   繁体   中英

Regex match a character that's not part of word in string

Using Regex in JS I'm trying to match all < and > which are not part of a specific HTML element. In the below example, I try to exclude those associated with <sup> and </sup> but my current expression still matches with > at the end of the element.

How can I also exclude the ending > of the element?

Current expression:

 /(?!<\/?sup>)[<>]/gi

正则表达式示例的图像

Almost there; the special tag can show up before or after a tag, eg using ( Demo ):

(?<!sup)[<>](?!\/?sup)

Or if you want to make it more readable: <(??\/?sup)|(?<!\/?sup)> We could optimize this with possessive quantifiers; unfortunately, JS does not support them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM