RegExp.escape

C. Scott Ananian ecmascript at cscott.net
Fri Mar 21 08:38:13 PDT 2014


One more for the "it's too late for ES6" train: most other programming
languages have a convenient "safe" way to turn a string into a regular
expression matching that string.  `RegExp.escape` is often suggested
as the function name.  I think this is worth adding to the standard
library because it helps patch a ["candy machine
interface"](http://www.approxion.com/?p=123) -- that is, I've seen the
following too often (including again today, hence this message):

```js
function replaceTitle(title, str) {
  return str.replace(new RegExp(title), "...");
}
```

There ought to be a standard simple way of writing this correctly.

Has this been discussed in the context of ES6/ES7 before?
 --scott


More information about the es-discuss mailing list