Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
st-clair-clarke opened this issue Nov 16, 2023 · 2 comments
Closed

Comments

@st-clair-clarke
Copy link

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
Copy link
Owner

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
Copy link
Author

Thanks. Option 2 seems the better of the two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants