How can I match double-quoted strings with escaped double-quote characters?

/"(?:[^\\"]|\\.)*"/

This is almost the same as Cal’s answer, but has the advantage of matching strings containing escape codes such as \n.

The ?: characters are there to prevent the contained expression being saved as a backreference, but they can be removed.

NOTE: as pointed out by Louis Semprini, this is limited to 32kb texts due a recursion limit built into Perl’s regex engine (that unfortunately silently returns a failure when hit, instead of crashing loudly).

Leave a Comment