Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Short 'n Sweet</h1> <pre><code>function escapeRegExp(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&amp;'); // $&amp; means the whole matched string } </code></pre> <p><strong>Example</strong></p> <pre><code>escapeRegExp("All of these should be escaped: \ ^ $ * + ? . ( ) | { } [ ]"); &gt;&gt;&gt; "All of these should be escaped: \\ \^ \$ \* \+ \? \. \( \) \| \{ \} \[ \] " </code></pre> <p><strong>Install</strong></p> <p>Available on npm as <a href="https://www.npmjs.com/package/escape-string-regexp" rel="noreferrer">escape-string-regexp</a></p> <pre><code>npm install --save escape-string-regexp </code></pre> <p><strong>Note</strong></p> <p>See <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions" rel="noreferrer">MDN: Javascript Guide: Regular Expressions</a></p> <p>Other symbols (~`!@# ...) MAY be escaped without consequence, but are not required to be.</p> <p>.</p> <p>.</p> <p>.</p> <p>.</p> <h2>Test Case: A typical url</h2> <pre><code>escapeRegExp("/path/to/resource.html?search=query"); &gt;&gt;&gt; "\/path\/to\/resource\.html\?search=query" </code></pre> <h1>The Long Answer</h1> <p>If you're going to use the function above at least link to this stack overflow post in your code's documentation so that it doesn't look like crazy hard-to-test voodoo.</p> <pre><code>var escapeRegExp; (function () { // Referring to the table here: // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/regexp // these characters should be escaped // \ ^ $ * + ? . ( ) | { } [ ] // These characters only have special meaning inside of brackets // they do not need to be escaped, but they MAY be escaped // without any adverse effects (to the best of my knowledge and casual testing) // : ! , = // my test "~!@#$%^&amp;*(){}[]`/=?+\|-_;:'\",&lt;.&gt;".match(/[\#]/g) var specials = [ // order matters for these "-" , "[" , "]" // order doesn't matter for any of these , "/" , "{" , "}" , "(" , ")" , "*" , "+" , "?" , "." , "\\" , "^" , "$" , "|" ] // I choose to escape every character with '\' // even though only some strictly require it when inside of [] , regex = RegExp('[' + specials.join('\\') + ']', 'g') ; escapeRegExp = function (str) { return str.replace(regex, "\\$&amp;"); }; // test escapeRegExp("/path/to/res?search=this.that") }()); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload