I have the following regexp: (["'])(\\\1|[^\1])+\1
Obviously it could not be compiled because [^\1]
is illeagal.
Is it possible to negate a matched group?
You can't use backreferences in a positive or negative character class.
But you can achieve what you want using negative lookahead assertions:
(["'])(?:\\.|(?!\1).)*\1
Explanation:
(["']) # Match and remember a quote.
(?: # Either match...
\\. # an escaped character
| # or
(?!\1) # (unless that character is identical to the quote character in \1)
. # any character
)* # any number of times.
\1 # Match the corresponding quote.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With