Regex-not selector
Lets say that this whole thing down there is a string and I would like to match all those words within < and > who end with words dog or bird. It can be done also done by using regex like this:
<.+(dog|bird)>
But I am trying to select by using NOT selector. Only if it NOT ends with cat, it should be selected. Is it actual开发者_开发问答ly possible to select something that doesn't contain come word in regex?
<This is dog>
<This is also a dog>
<This is cat>
<Dog and cat>
<Where is dog>
<I am a bird>
<I am another bird>
Thanks,
Vizualni.
If we're talking about a flavor of regex that supports it, you can use negative lookahead. This:
<(?![^>]*(?:dog|cat)>).+>
will match angle-bracket-enclosed strings that do not end with dog
or cat
.
In addition to negative lookahead, you can also use negative lookbehind (match ">" that is not preceded by dog or cat):
<.+(?<!dog|cat)>
精彩评论