Search code examples
javascriptregexlookbehind

RegExp lookbehind assertion alternative


Given the string below, how would you split this into an array containing only the double quoted strings (ignoring nested quoted strings) without using a lookbehind assertion?

source string: 1|2|3|"A"|"B|C"|"\"D\"|\"E\""

target array:

[
  '"A"',
  '"B|C"',
  '"\"D\"|\"E\""'
]

Basically, I'm trying to find an alternative to /(?<!\\)".*?(?<!\\)"/g since Firefox currently doesn't support lookbehinds. The solution doesn't have to use regular expressions, but it should be reasonably efficient.


Solution

  • Just find all the quoted text /"[^"\\]*(?:\\[\S\s][^"\\]*)*"/g
    Don't need split for this.

    https://regex101.com/r/r5SJsR/1

    Formatted

     "
     [^"\\]*                       # Double quoted text
     (?: \\ [\S\s] [^"\\]* )*
     "