Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of all characters that should be escaped before put in to RegEx?

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

I fear I don't know some of them.

like image 748
Somebody Avatar asked Feb 24 '11 13:02

Somebody


People also ask

Which characters must be escaped in regex?

Operators: * , + , ? , | Anchors: ^ , $ Others: . , \ In order to use a literal ^ at the start or a literal $ at the end of a regex, the character must be escaped.

Do I need to escape period in regex?

(dot) metacharacter, and can match any single character (letter, digit, whitespace, everything). You may notice that this actually overrides the matching of the period character, so in order to specifically match a period, you need to escape the dot by using a slash \. accordingly.

What characters need to be escaped grep?

Whenever you use a grep regular expression at the command prompt, surround it with quotes, or escape metacharacters (such as & ! . * $ ? and \ ) with a backslash ( \ ).

What is escaping in regex?

The backslash in a regular expression precedes a literal character. You also escape certain letters that represent common character classes, such as \w for a word character or \s for a space.


1 Answers

Take a look at PHP.JS's implementation of PHP's preg_quote function, that should do what you need:

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

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

like image 159
Tatu Ulmanen Avatar answered Oct 04 '22 14:10

Tatu Ulmanen