Mathias Bynens mathias at
Fri Mar 21 08:59:29 PDT 2014

On 21 Mar 2014, at 16:38, C. Scott Ananian <ecmascript at> wrote:

> ```js
> function replaceTitle(title, str) {
>  return str.replace(new RegExp(title), "...");
> }
> ```
> There ought to be a standard simple way of writing this correctly.

I’ve used something like this in the past:

    RegExp.escape = function(text) {
      return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');

It escapes some characters that do not strictly need escaping to avoid bugs in ancient JavaScript engines. A standardized version could be even simpler, and would indeed be very welcome IMHO.

More information about the es-discuss mailing list