RegExp.escape()

Richard Gibson richard.gibson at gmail.com
Wed Dec 6 02:56:06 UTC 2017


Or even better: `(?]**foo)` ("]" still terminates character classes; "**"
is a less ugly normally-invalid sequence).

On Tue, Dec 5, 2017 at 11:25 AM, <richard.gibson at gmail.com> wrote:

> Reviving this [thread] a third time, is there any love left for
> introducing RegExp.escape? The previous attempt was abandoned because of a
> so-called "[even-odd problem]", but that can be fixed: backslash-escape
> every _SyntaxCharacter_, then wrap the full result in a new form of
> non-capturing group that is only valid **as a unit** (and therefore
> protected from otherwise dangerous preceding fragments). For example,
> `(?](?)…)` is a good candidate because preceding such content with a right
> bracket (starting a _CharacterClass_) and/or a backslash (escaping special
> treatment of the initial parenthesis) would produce invalid syntax by
> exposing the "(?)".
>
> As a result, `new RegExp(RegExp.escape("foo.bar"))` is valid (i.e.,
> `/(?](?)foo\.bar)/`, equivalent in evaluation to `/(?:foo\.bar)/`) but `new
> RegExp("\\" + RegExp.escape("foo.bar"))` and even `new RegExp("([\\" +
> RegExp.escape("foo.bar"))` would throw SyntaxErrors.
>
> The upside of such a change is getting safe access to desired language
> functionality. The downside, of course, is the new pattern's supreme
> ugliness.
>
> Sample polyfill:
> ```
> const regExpSyntaxCharacter = /[\^$\\.*+?()[\]{}|]/g;
> RegExp.escape = function( value ) {
>         return "(?](?)" + (value + "").replace(regExpSyntaxCharacter,
> "\\$&") + ")";
> }
> ```
>
> [thread]: https://esdiscuss.org/topic/regexp-escape
> [even-odd problem]: https://github.com/benjamingr/RegExp.escape/issues/37
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171205/4fac803b/attachment.html>


More information about the es-discuss mailing list