slevithan / xregexp

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

console.log showing subpattern in regex instead of the actual regex #359

Closed st-clair-clarke closed 11 months ago

st-clair-clarke commented 11 months ago

I have the following:

 const baseRegex = XRegExp.tag('gimx')`A-Z0-9`
console.log({baseRegex})  // expected => {baseRegex: /A-Z0-9/gim} 

const interimRegex = XRegExp.tag(baseRegex.flags)`[${baseRegex}]+`
console.log({interimRegex}) // UNEXPECTED => {interimRegex: /[{{subpattern0}}]+/gim}

Why does the interpolation ${baseRegex} results in {{subpattern0}}. From previous discussions, it should not.

slevithan commented 11 months ago

It's bug #192 that's been there since the introduction of XRegExp.tag, and which prevents interpolating regexes inside character classes. Two potential paths forward are mentioned in that issue:

  1. Making interpolation into a character class a syntax error (to avoid unexpected surprises like breaking out of a character class with an unescaped ], or the meaning of regex symbols changing in character class context).
  2. Matching probably most users' preferences by allowing it in the way you're expecting here (possibly with some level of protection).

Pull requests are welcome.

st-clair-clarke commented 11 months ago

Thanks. Option 2 seems the better of the two.