-1

I am trying to build a regex which is able to match double quoted strings. Here is what I have:

regex = /\"(\\\"|[^\"])*\"/g

It works for almost all cases. But there is a case for which it does not capture the string correctly. For instance if the input is "\" it should not capture it as a double quoted string, since \" would be a the character '"'. For this purpose I tried to figure out the condition required, which it is:

  • If the previous character of the closing double quote is '\' do not capture it. But honestly I don't know how to express this into the regex expression.

I would appreciate some tips and help to get this done. Thanks beforehand

Liam
  • 27,717
  • 28
  • 128
  • 190
ggaliero
  • 1
  • 2
  • 1
    I think `"(?:[^\\"]+|\\.)*"` is the better approach to this. – Sebastian Proske May 23 '17 at 13:41
  • [Please don't put tags in question titles](https://stackoverflow.com/help/tagging) – Liam May 23 '17 at 13:58
  • Possible duplicate of [JavaScript text between double quotes](https://stackoverflow.com/questions/19793221/javascript-text-between-double-quotes) – Scott Weaver May 23 '17 at 15:00
  • By the way, double-quotes don't need to be escaped when using the `/pattern/` constructor, so `\"` and `\\\"` could be simply written `"` and `\\"`. – Aaron May 23 '17 at 15:38

1 Answers1

0

Try this: (?:[^\\\"]|^)\"([^\"]*)\"

Xyzk
  • 1,332
  • 2
  • 21
  • 36