Your task is to write a RegEx that matches everything inside strings.

A string is defined as everything surrounded by (but not including) two unescaped ".

A " can be escaped by \, which can also be escaped again.


string:  ab\c"defg\\\"hi"jkl"mn\\\\"opqrst""
matches:      ^^^^^^^^^^     ^^^^^^        ^ (the empty string)


Shortest solution wins.


  • Please specify the flavour used.
  • The input will have balanced ".
  • There will be no \ that immediately precedes a string-beginning-delimiter. For example, you would not need to handle abc\"def"
  • 1
    \$\begingroup\$ Will there be \ before a string? For example abc\"def". \$\endgroup\$
    – jimmy23013
    Commented May 20, 2016 at 10:29
  • \$\begingroup\$ Should it match each string in one group? For example, could I write something that has two matches in abc"de", one is d and the other is e? \$\endgroup\$
    – jimmy23013
    Commented May 20, 2016 at 10:43
  • \$\begingroup\$ It is allowed . \$\endgroup\$
    – Leaky Nun
    Commented May 20, 2016 at 10:44
  • \$\begingroup\$ Will there be empty strings? \$\endgroup\$ Commented May 20, 2016 at 10:45
  • \$\begingroup\$ Yes, there will be empty strings. \$\endgroup\$
    – Leaky Nun
    Commented May 20, 2016 at 10:48

3 Answers 3


PCRE, 21 20 15 19 bytes


Try it here.

This matches a character (or the beginning of the input) before the beginning double quote and then reset the match, to make sure the double quote isn't shared with another match.

PCRE, 25 23 bytes

Thanks to Martin Büttner for golfing off 2 bytes.


Try it here.


    \\.|[^"]     # An escaped character, or a character that isn't a double quote
)*+              # Possessive zero-or-more quantifier, which means backtracking
                 # could not happen after first match is found. That means if \\.
                 # matched, it would never switch to [^"], because it is always a
                 # match if it just stopped after the \\. without backtracking.
(?!"(?R)|$)      # Make sure it is not followed by a double quote and another
                 # match, or the end of the input.

Note that the possessive quantifier (*+) made sure the negative lookahead always begins after a whole string, or a whole segment of non-string.

There are 4 cases:

  • The match begins anywhere outside of a string. \\. would never match a double quote according to the clarification. It could only end just before the next double quote which begins a string, or the end of input. Both cases fails the negative lookahead.
  • The match begins at the beginning of a string. (\\.|[^"])*+ would match a complete string. The next character must be a double quote, and couldn't be the end of input. After the double quote it is outside of the string, so it couldn't be another match. So it passes the negative lookahead.
  • The match begins at the end of a string. It matches an empty string in the same way as the previous case. But it doesn't matter according to the clarification.
  • The match begins in the middle of a string. Impossible because matches don't overlap.
  • \$\begingroup\$ Would (\\.|[^"]) work? \$\endgroup\$ Commented May 20, 2016 at 10:50
  • \$\begingroup\$ @MartinBüttner that matches everything except " \$\endgroup\$
    – Bálint
    Commented May 20, 2016 at 10:52
  • \$\begingroup\$ @Bálint I meant in place of ([^\\"]|\\.), not as the complete solution. \$\endgroup\$ Commented May 20, 2016 at 10:52
  • \$\begingroup\$ @MartinBüttner Oh, ok \$\endgroup\$
    – Bálint
    Commented May 20, 2016 at 10:56
  • \$\begingroup\$ Martin's suggestion should work, since \\. only fails when there is no character after \ (or new line character, but that can be fixed with flag), and that case is covered by the negative look-behind. The possessive quantifier prevents backtracking, so we have no other case to look at. \$\endgroup\$ Commented May 20, 2016 at 11:03

JavaScript, 24 bytes


Group 1 is the contents of the string.

  • \$\begingroup\$ This doesn't at all work with escaped quotes, and thus fails to meet the spec. \$\endgroup\$
    – ATaco
    Commented Feb 15, 2017 at 21:12
  • \$\begingroup\$ Ah yes--sorry. How about that? \$\endgroup\$ Commented Feb 15, 2017 at 22:20
  • \$\begingroup\$ Close but no cigar, you shouldn't be matching the outer "s \$\endgroup\$
    – ATaco
    Commented Feb 15, 2017 at 22:22
  • \$\begingroup\$ Yeah, that's what I was afraid of. No way of doing it in JavaScript, I'm guessing? \$\endgroup\$ Commented Feb 15, 2017 at 22:52
  • \$\begingroup\$ You can capture it in a subgroup \$\endgroup\$
    – ATaco
    Commented Feb 15, 2017 at 22:53

JavaScript, 21 15 13 12 bytes


String contents are in group 1.

"   #start of string
(    #capturing group
  \\?. #match character or escaped character
 )*?  #match as few as possible
"   #end of string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.