List of all characters that must be escaped before entering RegEx?

Can someone please give a complete list of special characters that should be escaped?

I am afraid that I do not know some of them.

+58
javascript regex escaping
Feb 24 '11 at 13:13
source share
6 answers

Take a look at the PHP.JS implementation of the PHP function preg_quote , which should do what you need:

http://phpjs.org/functions/preg_quote:491

Special regular expression characters:. . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -

+62
Feb 24 '11 at 13:19
source share

According to this site a list of characters to exit

[, backslash \, carriage ^, dollar sign $, period or dot., vertical bar or pipe symbol, question mark ?, asterisk or star *, plus sign +, open round bracket (and closing round bracket).

In addition to this, you need to avoid characters that are interpreted by the Javascript interpreter as the end of a string, either ' , or " .

+9
Feb 24 '11 at 13:16
source share

A hyphen (-) must be escaped when it is in square brackets, and it is not located at the beginning or at the end, for example You must run - for

 [a-z0-9\-_]+ 

No need to run - for

 [a-z0-9_-]+ 
+4
May 25 '16 at 15:55
source share

Based on Tatu Ullman's answer, my solution in C # got the following form:

 private static List<string> RegexSpecialCharacters = new List<string> { "\\", ".", "+", "*", "?", "[", "^", "]", "$", "(", ")", "{", "}", "=", "!", "<", ">", "|", ":", "-" }; foreach (var rgxSpecialChar in RegexSpecialCharacters) rgxPattern = input.Replace(rgxSpecialChar, "\\" + rgxSpecialChar); 

Note that I switched the positions "\" and ".", Failure to handle slashes first will double the value "\"

+3
Jul 26 '17 at 11:16
source share

I searched this list regarding the installation of ESLint "no-noeless-escape" for reg-ex. And I found that some of these characters do not need acceleration for regular expression in JS. A longer list in another answer here is for PHP, which requires extra characters to be escaped.

In this github issue for ESLint , about halfway down, the not-an-aardvark explains why the character referenced by the issue is a character that could possibly be escaped.

In javascript, a character that SHOULD slip away is a character or one of them:

^ $ \ . * + ? ( ) [ ] { } |

The answer to the github problem I linked to above includes an explanation of the semantics of "Appendix B" (which I know little about), which allows us to exclude 4 of the above characters:) ) ] { } .

Another thing to note is that escaping a character that does not require escaping will not do any harm (except maybe if you are trying to avoid an escape character). So, my personal rule of thumb: "When in doubt, escape"

0
Dec 15 '17 at 18:45
source share

This problem:

 const character = '+' new RegExp(character, 'gi') // error 

Smart solutions:

 // with babel-polyfill // Warning: will be removed from babel-polyfill v7 const character = '+' const escapeCharacter = RegExp.escape(character) new RegExp(escapeCharacter, 'gi') // /\+/gi // ES5 const character = '+' const escapeCharacter = escapeRegExp(character) new RegExp(escapeCharacter, 'gi') // /\+/gi function escapeRegExp(string){ return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') } 
0
Aug 21
source share



All Articles