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

Use case for overriding RegExp#exec (Type IV) #27

Open
slevithan opened this issue Aug 8, 2024 · 1 comment
Open

Use case for overriding RegExp#exec (Type IV) #27

slevithan opened this issue Aug 8, 2024 · 1 comment

Comments

@slevithan
Copy link

slevithan commented Aug 8, 2024

Subclassing RegExp and overriding exec is extremely useful for extending ECMAScript regular expressions. I think previous issues have pointed out that this is used by Babel. Other libraries using it include:

  • Regex+
    • Regex+ does this to support transpiling extended regex syntax for atomic groups and regex subroutines (from PCRE, Perl, etc.) into native JS regexes. Emulating these features requires the addition of "emulation groups" that add anonymous capturing groups to a regex but should not affect the results of regex matches (e.g. they should not affect backreferences in the replacement strings/callbacks of String#replace/replaceAll, nor should they add to result arrays from RegExp#exec or String#match/matchAll, nor alter the results of String#split).
  • Oniguruma-To-ES
    • Uses it for emulation of certain advanced Oniguruma regex features.
  • Shiki
    • Popular syntax highlighting library that uses Oniguruma-To-ES for its JavaScript engine.

Subclassing RegExp and overriding exec is also useful in backcompat libraries for polyfilling named capturing as well as flags /y and /d. And it will likely be useful for pollyfilling future RegExp flags and syntax. Essentially, it's a great way to make all regex methods and regex-using string methods benefit from the polyfill, rather than having to reimplement much slower versions of all of the methods.

The readme for this proposal currently makes it sound like overriding exec is asking for a bad time, but it’s actually amazingly easy to use.

@dead-claudia
Copy link

I agree and feel the type IV example would be better addressed by instead changing re.test(str) to directly return re.exec(str) != null and dropping all the symbol methods.

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