The Backslash \ Operator
When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary. For example, the following are equivalent:
- re = new RegExp("\\w+")
- re = /\w+/
For characters that are usually treated literally,\ indicates that the next character is special and not to be interpreted literally.
- For example, /b/ matches the character b. By placing a backslash in front of b, that is by using /\b/, the character becomes special to mean match a word boundary.
While for characters that are usually treated specially, \ indicates that the next character is not special and should be interpreted literally.
- For example, * is a special character that means 0 or more occurrences of the preceding character should be matched;
- For example, /a*/ means match 0 or more a's.
- To match * literally, precede the it with a backslash;
- For example, /a\*/ matches a*.