slevithan / xregexp

Extended JavaScript regular expressions
http://xregexp.com/
MIT License
3.31k stars 278 forks source link

Error: Unbalanced delimiter found in string #307

Closed vyushin closed 3 years ago

vyushin commented 3 years ago

XRegExp version: 4.4.0

I'm trying provide string with double quotes to matchRecursive method

const str = '"Hello, "World"!"';
XRegExp.matchRecursive(str, '"', '"', 'g');

this throws an error:

Error: Unbalanced delimiter found in string

The same case works with other symbols. For example:

const str = '{"hello": { "value": "world" }}';
XRegExp.matchRecursive(str, '{', '}', 'g') // Ok

How can I use matchRecursive to find matches in string containing double quotes?

slevithan commented 3 years ago

XRegExp.matchRecursive in its current form doesn't work with identical left and right delimiters. That said, you can adapt your task so the positions your delimiters match are not identical with something like this:

const str = '"Hello, "World"!"';
XRegExp.matchRecursive(str, '"(?=\\w)', '"(?!\\w)', 'g');
// -> ['Hello, "World"!']