Skip to content

Instantly share code, notes, and snippets.

@ashumeow
Forked from kangax/gist:9698100
Last active August 29, 2015 14:14
Show Gist options
  • Save ashumeow/606fb316f4c2affdbf2e to your computer and use it in GitHub Desktop.
Save ashumeow/606fb316f4c2affdbf2e to your computer and use it in GitHub Desktop.

RegExp.escape(string)

Computes a new version of a String value in which certain characters have been escaped, so that the regular expression engine will interpret any metacharacters that it may contain as character literals.

When the escape function is called with one argument string, the following steps are taken:

  1. Let string be ToString(string).
  2. ReturnIfAbrupt(string).
  3. Let length be the number of characters in string.
  4. Let R be the empty string.
  5. Let k be 0.
  6. Repeat, while k < length, 1. Let C be the character at position k within string. 1. If C is one of the 16 nonblank characters "-[]{}()*+?.,\^$|" then,
    1. Let S be a String containing two characters "\x" where x is a C character. 1. Else,
    2. Let S be a String containing the single C character. 1. Let R be a new String value computed by concatenating the previous value of R and S. 1. Increase k by 1.
  7. Return R.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment