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

Should throw when using XRegExp.replace with native named capture and numbered backref one higher than number of captures #317

Closed
slevithan opened this issue Jan 10, 2021 · 0 comments
Labels

Comments

@slevithan
Copy link
Owner

Found an edge case bug by reading through the code.

ES2018 added a new trailing groups arg that's passed to replacement functions when the search regex uses native named capture. XRegExp.replace wasn't yet accounting for this if all of the following conditions were in place:

  • Using an ES2018 runtime.
  • Using XRegExp.replace with a search regex that uses named capture and is a native regex (not constructed with XRegExp).
  • The replacement is a string rather than function.
  • The replacement string references a capturing group by number that is exactly one higher than the number of capturing groups in the search regex.

Repro:

XRegExp.replace('a', /(?<n>.)/, '$2');
XRegExp.replace('a', /(?<n>.)/, '$02');
XRegExp.replace('a', /(?<n>.)/, '$<2>');
XRegExp.replace('a', /(?<n>.)/, '${2}');
XRegExp.replace('a', /(?<n>.)/, '${002}');
// all should throw, but instead return ''

XRegExp replacement text syntax is stricter than native replacement text syntax in order to avoid/highlight unintended user error and avoid cross-browser inconsistencies/bugs. Native 'a'.replace(/(?<n>.)/, '$2') in fact returns '$2', but XRegExp.replace is supposed to improve this by throwing a SyntaxError.

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

No branches or pull requests

1 participant