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

Regex escaping #3330

Closed
lionel-rowe opened this issue Apr 25, 2023 · 2 comments · Fixed by #3334
Closed

Regex escaping #3330

lionel-rowe opened this issue Apr 25, 2023 · 2 comments · Fixed by #3334

Comments

@lionel-rowe
Copy link
Contributor

Is your feature request related to a problem? Please describe.

https://github.com/tc39/proposal-regex-escaping/ has been in TC39 purgatory for almost 8 years now and it's still at stage 1, despite what seems to be fairly wide consensus that it's a useful feature that should be standardized. In light of that, it seems like a good candidate for inclusion in a standard library, for later deprecation if and when the proposal ever gets rolled out.

Describe the solution you'd like

A function called something like regexEscape with an API something like this:

export declare function regexEscape(str: string): string

Ideally, it'd support contexts both within and outside of character classes, as well as all flavors of regex, including the upcoming v flag. It'd escape the minimal set of characters needed to be agnostic to all those contexts (i.e. no escaping of non-ASCII characters or always-literal characters like _), and for other characters, it would use the most readable escape where possible (e.g. \. for a literal period), falling back to hex escapes where that's impossible (e.g. \x3c for <).

Describe alternatives you've considered

Wait another 8 years I guess? 😅

Alternative APIs would involve versions that take options (e.g. specifying flags or whether the context is within a character class or not) or exporting different versions of the function for these use cases. Both of these options would make the implementation significantly more complex.

@lino-levan
Copy link
Contributor

I'm in favor of this proposal. @lionel-rowe are you willing to open a PR?

@lionel-rowe
Copy link
Contributor Author

@lionel-rowe are you willing to open a PR?

Happy to at some point, not sure when I'll get around to it though.

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

Successfully merging a pull request may close this issue.

2 participants