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

Fix interpolation within character classes for XRegExp.build/tag #192

Open
slevithan opened this issue Apr 27, 2017 · 0 comments
Open

Fix interpolation within character classes for XRegExp.build/tag #192

slevithan opened this issue Apr 27, 2017 · 0 comments

Comments

@slevithan
Copy link
Owner

slevithan commented Apr 27, 2017

Current behavior: XRegExp.build's {{name}} syntax is a literal string when used within character classes.

E.g., although XRegExp.build('{{x}}', {x: '.'}); produces /(?:.)/, XRegExp.build('[{{x}}]', {x: '.'}); returns /[{{x}}]/. Originally, this was intentional, to avoid unexpected behavior:

  • The syntax of an interpolated regex is written in normal regex scope, but then embedded in character class scope where the syntax rules and meaning are wildly different.
  • It can also lead to situations where you break out of a character class with unescaped ], introduce negation with unescaped ^ as the first character in the character class, or introduce unintended ranges (either with unescaped - as the first or last character, or because an unescaped - immediately preceded or followed the interpolation).

That was all justifiable until the recent introduction of XRegExp.tag in #180, which relies on XRegExp.build. Now, XRegExp.tag()`[${x}]`; returns /[{{subpattern0}}]/, which is just a bug. I see at least two approaches to fix this:

  1. Make the use of interpolation within a character class a syntax error in XRegExp.build, which would extend for free into XRegExp.tag. This is probably the easier and safer fix, and even for XRegExp.build it is at least better than the current behavior since it will better help people realize that this syntax isn't supported.
  2. Change course and support interpolation within character classes. This is probably the better path since, from most users' perspectives, it would simply add flexibility and avoid surprises. Perhaps XRegExp could guard against dangerous cases like an unescaped ] within the interpolated pattern by just detecting those cases and making them syntax errors.

Neither approach would be a breaking change.

Note that XRegExp.tag handles interpolated strings differently than interpolated regexes. Strings interpolated into an XRegExp.tag pattern automatically have their special regex characters escaped. Perhaps that should be reconsidered at the same time as fixing the interpolation of regexes into character classes, but it would be a breaking change.

@slevithan slevithan changed the title Improve interpolation within character classes for XRegExp.build/tag Fix interpolation within character classes for XRegExp.build/tag Apr 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant