There are quite a few similar questions already but none of them works in my case. I have a string that contains multiple substrings inside double quotes and these substrings can contain escaped double quotes.
For example for the string 'And then, "this is some sample text with quotes and \"escaped quotes\" inside". Not that we need more, but... "here is \"another\" one". Just in case.', the expected result is an array with two elements;
"this is some sample text with quotes and \"escaped quotes\" inside"
"here is \"another\" one"
The /"(?:\\"|[^"])*"/g
regex works as expected on regex101; however, when I use String#match()
the result is different. Check out the snippet below:
let str = 'And then, "this is some sample text with quotes and \"escaped quotes\" inside". Not that we need more, but... "here is \"another\" one". Just in case.'
let regex = /"(?:\\"|[^"])*"/g
console.log(str.match(regex))
Instead of two matches, I got four, and the text inside the escaped quotes is not even included.
MDN mentions that if the g
flag is used, all results matching the complete regular expression will be returned, but capturing groups will not. If I want to obtain capture groups and the global flag is set, I need to use RegExp.exec()
. I've tried it, the result is the same:
let str = 'And then, "this is some sample text with quotes and \"escaped quotes\" inside". Not that we need more, but... "here is \"another\" one". Just in case.'
let regex = /"(?:\\"|[^"])*"/g
let temp
let matches = []
while (temp = regex.exec(str))
matches.push(temp[0])
console.log(matches)
How could I get an array with those two matched elements?