Skip to content
Advertisement

How can I select text not in a group?

I want to select strings that are not in a $ ... $ group. I can choose the group itself, but it should be the other way around.

Example :

$ blah **deneme** blah $ xxx **bold** xxxxx $ blah **bold** blah $

First group is $ blah **deneme** blah $

Second group is $ blah **bold** blah $

I want to get this in regex **bold** so it must start with **, end with ** but cannot be a member of $...$ group

I wrote regex but what I needed would be to choose the opposite.

/(?:$)(.*?)[*_]{2}([^*_]+)[*_]{2}(.*?)(?:$)/gm

Advertisement

Answer

You may use this regex with a lookahead:

**S+?**(?=(?:(?:[^$]*$){2})*[^$]*$)

RegEx Demo

RegEx Details:

  • **: Match starting **
  • S+?: Match 1 or more characters that are not whitespace (lazy)
  • **: Match ending **
  • (?=(?:(?:[^$]*$){2})*[^$]*$): Lookahead to assert 0 or more pair of $..$ ahead to make sure we are not matching inside $...$
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement