Erik Corry (2012-03-23T17:26:07.000Z)
2012/3/23 Steven Levithan <steves_list at hotmail.com>:
> On Wednesday, Jan 04, 2012 at 8:03 PM, Kris Kowal wrote:
>>
>> On Sun, Jun 13, 2010 at 7:50 AM, Jordan Osete <jordan.osete at yahoo.fr>
>> wrote:
>>>
>>> Hello everybody.
>>>
>>> How about standardizing something like RegExp.escape() ?
>>> http://simonwillison.net/2006/Jan/20/escape/
>>>
>>> It is trivial to implement, but it seems to me that this functionality
>>> belongs to the language - the implementation obviously knows better
>>> which characters must be escaped, and which ones don't need to.
>>
>>
>> +1
>
>
> +1, again.
>
> Although this is only a minor convenience since you can do something like
> text.replace(/[-[\]{}()*+?.,\\^$|]/g, "\\$&"), the list of special
> characters is subject to change. E.g., if ES adds /x, whitespace (and
> possibly #) must be added.

In perl the recommended version is

text.replace(/([^a-zA-Z0-9])/g, "\\$1")

which is future-proof and safe and I think this also works for JS.

-- 
Erik Corry
domenic at domenicdenicola.com (2014-03-21T16:59:48.262Z)
In perl the recommended version is

```perl
text.replace(/([^a-zA-Z0-9])/g, "\\$1")
```

which is future-proof and safe and I think this also works for JS.