Description
openedon Jun 3, 2024
I would be happy to see RegExp.escape
exist (in fact, I advocated for it in several comments at the top of the years-old es-discuss thread mentioned in this proposal's motivation, and I’ve been shipping XRegExp
.escape
for > 15 years). But there are some advantages to escaping via interpolation in a template tag that have been discussed in depth in previous issues (including #37 and #45).
I created a new library, Regex.make, that among other features includes robust support for context-aware escaping via interpolation. It improves upon the 2015 proof of concept of this feature (regexp-make-js) by @mikesamuel and @erights in several ways, including full support of ES2024 regexes, avoiding or fixing various edge cases, and offering context-aware escaping, sandboxing, and atomization of interpolated values in a greater number of contexts (including e.g. on range or set operation boundaries and within enclosed tokens).
It additionally addresses the use case of composing a dynamic number of strings (among other use cases) via the concept of "partial pattern" strings that are interpolated in a context-aware way without escaping special regex characters.
I'm in the camp that a standardized template tag can coexist with a standardized RegExp.escape
. I'm hoping that sharing this here leads to additional insight/discussion (which I acknowledge might only be tangentially related to RegExp.escape
) or is at least an interesting reference point.
CC @domenic, @littledan, @ljharb.
Activity