matchRecursive fail in `
poyoho opened this issue · 1 comments
console.log(XRegExp.matchRecursive('`aaa ${``} `', '`', '`', 'g'));error message
xregexp/lib/addons/matchrecursive.js:260
throw new Error((0, _concat["default"])(_context3 = "Unbalanced ".concat(delimSide, " delimiter found in string at position ")).call(_context3, errorPos));
^
Error: Unbalanced left delimiter found in string at position 0
The error is accurate and appropriate. Your string contains four left delimiters, all of them unbalanced (the first one being at position 0).
What it seems you're intending to match is /`(?:[^`]|`[^`]*`)*`/, which does not require XRegExp.matchRecursive. However, note that since there is no distinguishing between left and right delimiters, this will match from the first to the last grave accent so long as there are an even number in the string. I.e., there is no way to get two or more distinct matches of independent nested patterns from the same string.
Or to give an example, say your target string is '`1`2`1`'. The regex will match that just fine. But now you want to add '`3`4`3`' to the string and separate it from the first set of nested grave accents by a few other words. So you end up with the target string '`1`2`1` hello world `3`4`3`' and expect the regex to find two matches. Uh, no. You'll get one match extending from the very first to the very last grave accent, because there is no way to determine when one of these nested groups ends if there are an even number of additional grave accents in the string (at least, with the information written into the regex so far).
If you're content to support only one level of nesting, then e.g. /`[^`]*(?:`[^`]*`)?[^`]*`/ could do the trick.