slevithan/xregexp

Question about using matchRecursive and in an unbalanced string

moopmonster opened this issue · 1 comments

Other than using escapeChar to escape the stray ">", or changing the lessthan/greaterthan bracket to braces {} , are there any other ways to use matchRecursive on a string like : $<var1> == 1 && $<var2> > 0 so that the match only gives var1 and var2 and does not throw an unbalanced delimiter error?

Example snipplet below:

x = "$<var1> == 1 && $<var2> > 0";
XRegExp.matchRecursive(str,"\\$<", ">", "g");

Thank you for your help.

For this example, it's not clear that XRegExp.matchRecursive is the right solution since it doesn't seem like you'd ever run into an input string like $< ... $<...>>.

Maybe you could use something like this instead?

let str = "$<var1> == 1 && $<var2> > 0";
str.split(/(\$<[^>]*>)/);
// -> ["", "$<var1>", " == 1 && ", "$<var2>", " > 0"]

Or:

XRegExp.matchChain(str, [
  {regex: /\$<([^>]*)>/, backref: 1}
]);
// -> ["var1", "var2"]

In general, though, your question is a good one and I'd love for XRegExp.matchRecursive to have an option for handling unbalanced delimiters differently (without throwing). PRs welcome! See #96 which is tracking this.