Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

33
  • 25
    actually, we don't need to escape / at all Commented Feb 14, 2013 at 20:53
  • 43
    @Paul: Perl quotemeta (\Q), Python re.escape, PHP preg_quote, Ruby Regexp.quote... Commented Oct 3, 2013 at 10:24
  • 20
    If you are going to use this function in a loop, it's probably best to make the RegExp object it's own variable var e = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g; and then your function is return s.replace(e, '\\$&'); This way you only instantiate the RegExp once. Commented Oct 17, 2013 at 21:14
  • 42
    bobince cares not for eslint's opinion Commented Sep 15, 2017 at 22:57
  • 11
    But maybe you want to escape characters to put them inside a character range. IMO better to harmlessly overescape than to underescape and cause problems in niche cases. FWIW personally I'd rather see the characters explicitly here; we're not playing code golf. Commented Oct 12, 2017 at 20:54