Table of Contents

  1. 1 RegExp.escape ( S )

1RegExp.escape ( S )

escape takes a string S and returns a version of it with all control characters escaped. It escapes characters that would otherwise be treated by the regular expressions engine as special meta characters such as ^ $ \ . * + ? ( ) [ ] { } | using an escape sequence character.

When the escape function is called with an argument S the following steps are taken:

  1. Let str be ToString(S).
  2. ReturnIfAbrupt(str).
  3. Let cpList be a List containing in order the code points as defined in 6.1.4 of str, starting at the first element of str.
  4. Let cuList be a new List
  5. For each code point c in cpList in List order, do:
    1. If c is a SyntaxCharacter then do:
      1. Append code unit 0x005C (REVERSE SOLIDUS) to cuList.
    2. Append the elements of the UTF16Encoding (10.1.1) of c to cuList.
  6. Let L be a String whose elements are, in order, the elements of cuList.
  7. Return L.

The length property of the escape method is 1.

Note

escape takes a string and escapes it so it can be literally represented as a pattern. In contrast EscapeRegExpPattern (as the name implies) takes a pattern and escapes it so that it can be represented as a string. While the two are related they do not share the same character escape set or perform similar actions.