Regex NOT Operation

2019-09-10 02:34发布

问题:

I have condition where I have to select anything which is not part of span tag.

Input -

the <span class='ptc-highlightedSearchResult'>PISTON</span> has their <span class='ptc-highlightedSearchResult'>ROD</span> ring

regex which selects <span> tag and it's content -

(<span[^>]+class\s*=\s*("|')ptc-highlightedSearchResult\2[^>]*>)[^<]*(</span>)

I'm able to select whatever comes in span and their content but not otherwise. Any help on NOT operation will be appreciated.

回答1:

You can use this:

((?:(?![^<>]*(?:>))[^<](?![^<>]*</))+)

regex101 demo

It will match any text not inside or between opening and closing tags. There's a breakdown of the regex in the demo.