If your strings have no escape sequences, it is as easy as using a tempered greedy token like
/(['"])(?:(?!\1)[\s\S])+\1/g
See the regex demo. The (?:(?!\1)[\s\S])+
matches any symbol ([\s\S]
) that is not the value captured into Group 1 (either '
or "
). To also match ""
or ''
, replace the +
(1 or more occurrences) with *
quantifier (0 or more occurrences).
If you may have escape sequences, you may use
/(['"])(?:\\[\s\S]|(?!\1)[^\\])*?\1/g
See this demo.
See the pattern details:
(['"])
- Group 1 capturing a '
or "
(?:\\[^]|(?!\1)[^\\])*?
- 0+ (but as few as possible) occurrences of
\\[^]
- any escape sequence
|
- or
(?!\1)[^\\]
- any char other than \
and the one captured into Group 1
\1
- the value kept in Group 1.
NOTE: [\s\S]
in JS matches any char including line break chars. A JS only construct that matches all chars is [^]
and is preferable from the performance point of view, but is not advised as it is not supported in other regex flavors (i.e. it is not portable).