regex match text in either single or double quote

Very sad, but such an elegant and accurate way does not work:

(["'])(?:\\\1|[^\1]+)*\1

But we can change it a little bit, and all works fine:

(["'])((?:\\\1|(?:(?!\1)).)*)(\1)

https://regex101.com/r/dKdBMT/2

I would like to make sure that this regexp will work in all cases: please more test it.


This one seems to work:

(?:'|").*(?:'|")

or

((?:'|").*(?:'|"))

if you need a group.

Here's the demo: link

It works, because * is a greedy quantifier, so you don't have to know what kind of quote is in the end. * will take as much as possible.

Tags:

Regex