Skip to main content
there's a date at the bottom of the answer
Source Link
miken32
  • 42.5k
  • 16
  • 127
  • 177

Short 'n Sweet (Updated 2021)

Short 'n Sweet (Updated 2021)

fix typo
Source Link
root-aj
  • 77.6k
  • 23
  • 117
  • 135

Short 'n Sweet (Updated 2021)

function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } 

To escape the RegExp itself:

function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } 

Example To escape a replacement string:

escapeRegExp("All of these should be escaped: \ ^ $ * + ? . ( ) | { } [ ]"); >>> "All of these should be escaped: \\ \^ \$ \* \+ \? \. \( \) \| \{ \} \[ \] " 
function escapeReplacement(string) { return string.replace(/\$/g, '$$$$'); } 

Example

All escaped RegExp characters:

escapeRegExp("All of these should be escaped: \ ^ $ * + ? . ( ) | { } [ ]"); 
>>> "All of these should be escaped: \\ \^ \$ \* \+ \? \. \( \) \| \{ \} \[ \] " 

Find & replace a string:

var haystack = "I love $x!"; var needle = "$x"; var safeNeedle = escapeRegExp(needle); // "\\$x" var replacement = "$100 bills" var safeReplacement = escapeReplacement(replacement); // "$$100 bills" haystack.replace( new RegExp(safeNeedle, 'g'), escapeReplacement(safeReplacement), ); // "I love $100 bills!" 

Short 'n Sweet

function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } 

Example

escapeRegExp("All of these should be escaped: \ ^ $ * + ? . ( ) | { } [ ]"); >>> "All of these should be escaped: \\ \^ \$ \* \+ \? \. \( \) \| \{ \} \[ \] " 

Short 'n Sweet (Updated 2021)

To escape the RegExp itself:

function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } 

To escape a replacement string:

function escapeReplacement(string) { return string.replace(/\$/g, '$$$$'); } 

Example

All escaped RegExp characters:

escapeRegExp("All of these should be escaped: \ ^ $ * + ? . ( ) | { } [ ]"); 
>>> "All of these should be escaped: \\ \^ \$ \* \+ \? \. \( \) \| \{ \} \[ \] " 

Find & replace a string:

var haystack = "I love $x!"; var needle = "$x"; var safeNeedle = escapeRegExp(needle); // "\\$x" var replacement = "$100 bills" var safeReplacement = escapeReplacement(replacement); // "$$100 bills" haystack.replace( new RegExp(safeNeedle, 'g'), escapeReplacement(safeReplacement), ); // "I love $100 bills!" 
Added note explaining why the top part of this answer disagrees with the rest of it.
Source Link
Darren Cook
  • 29.2k
  • 13
  • 127
  • 239

(NOTE: the above is not the original answer; it was edited to show the one from MDN. This means it does not match what you will find in the code in the below npm, and does not match what is shown in the below long answer. The comments are also now confusing. My recommendation: use the above, or get it from MDN, and ignore the rest of this answer. -Darren,Nov 2019)

Install

Install

(NOTE: the above is not the original answer; it was edited to show the one from MDN. This means it does not match what you will find in the code in the below npm, and does not match what is shown in the below long answer. The comments are also now confusing. My recommendation: use the above, or get it from MDN, and ignore the rest of this answer. -Darren,Nov 2019)

Install

match MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Source Link
Timo Huovinen
  • 56k
  • 34
  • 163
  • 150
Loading
Updated to 1.0.5 https://github.com/sindresorhus/escape-string-regexp/blob/master/index.js https://www.npmjs.com/package/escape-string-regexp
Source Link
Timo Huovinen
  • 56k
  • 34
  • 163
  • 150
Loading
note npm
Source Link
root-aj
  • 77.6k
  • 23
  • 117
  • 135
Loading
code snippet and explanation has been moved to another site on MDN
Source Link
Aristotle Pagaltzis
  • 119.4k
  • 24
  • 102
  • 101
Loading
added 3 characters in body
Source Link
noob
  • 9.2k
  • 4
  • 40
  • 65
Loading
Please leave the headers. It's much more readable that way.
Source Link
root-aj
  • 77.6k
  • 23
  • 117
  • 135
Loading
Rollback to Revision 1
Source Link
root-aj
  • 77.6k
  • 23
  • 117
  • 135
Loading
Rollback to Revision 2
Source Link
root-aj
  • 77.6k
  • 23
  • 117
  • 135
Loading
was missing an escape (caught by jshint)
Source Link
root-aj
  • 77.6k
  • 23
  • 117
  • 135
Loading
deleted 49 characters in body
Source Link
casperOne
  • 74.7k
  • 19
  • 189
  • 262
Loading
Source Link
root-aj
  • 77.6k
  • 23
  • 117
  • 135
Loading