RegExp. quoteText()

Axel Rauschmayer axel at rauschma.de
Wed Aug 28 08:23:59 PDT 2013


This must have been suggested before, but it would be great to have a built-in function for quoting text in a RegExp.

For example:

```js
RegExp.quoteText = function (text) {
    return text.replace(/[\^\$\\.*+?()[\]{}|]/g, '\\\$&');
};

```

If you wanted to be extra thorough, you could include parens, braces and brackets.

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130828/0859eb58/attachment.html>


More information about the es-discuss mailing list