3

Firstly we have the following string:

aaa{ignoreme}asdebla bla f{}asdfdsaignoreme}asd

We want our regex to find the whitespaces and any special charsacters like {}, but if after { comes exactly ignoreme} then exclude it

This is where we are right now:

(?!{ignoreme})[\s\[\]{}()<>\\'"|^`]

The problem is that our regex finds the } after ignoreme

Here is the link https://regex101.com/r/bU1oG0/2

Any help is appreciated, Thanks

1 Answers1

5

The point is that the } is matched since your (?!{ignoreme}) lookahead only skips a { followed with ignoreme} and matches a } since it is not starting a {ignoreme} char sequence. Also, in JS, you cannot use a lookbehind, like (?<!{ignoreme)}.

This is a kind of issue that can be handled with a regex that matches what you do not need, and matches and captures what you need:

/{ignoreme}|([\s[\]{}()<>\\'"|^`])/g

See the regex demo

Now, {ignoreme} is matched (and you do not have to use this value) and ([\s[]{}()<>\\'"|^`]) is captured into Group 1 the value of which you need to use.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 2
    I am waiting for "but I need..." comment. – Wiktor Stribiżew Aug 03 '16 at 11:09
  • No "but I need ..." comment because I had a wrong approach to the problem. I wanted to make it with negated lookahead (?!). This is way much easier. – Matzy schneider Aug 03 '16 at 11:18
  • Yes, the point is that you actually needed a lookbehind, but JS regex engine does not support this construct. :( – Wiktor Stribiżew Aug 03 '16 at 11:22
  • 1
    It is a different pattern and input, but the same construct and concept: the `(?!{{ignoreme})` lookahead restricts a generic character class following it in the pattern so that the character class cannot match a character that starts the character sequence specified inside the lookahead. It is part of the [**tempered greedy token**](http://stackoverflow.com/a/37343088/3832970), you can read a bit more on that there. So, the first `{` in `aaa{{ignoreme}asd` is not matched, but the second `{` after the first one is matched because it does not start a `{{ignoreme}` char sequence. – Wiktor Stribiżew Aug 03 '16 at 11:34
  • Thanks for everything, Have a nice day – Matzy schneider Aug 03 '16 at 11:35