How to match all characters that aren't from a capturing group?

34 Views Asked by At

I want to match all characters except those in a capturing group. Here's a minimal regex to serve as an example of what I want to achieve:

/(?:(["'])[^\1]*\1)/g

The obvious issue is that [^\1] doesn't work like that, it isn't using the capturing group. I can't find a way to implement that behavior.

1

There are 1 best solutions below

0
JoelFan On

I think what you really need is a "non-greedy" operator, such as .*?

(Also, why do you need the outer non-capturing parens?)

Try: /(["']).*?\1/g