Regular Expressions and Validation

Page 3 - Escaping Characters

Skip to navigation

Regular Expressions use a backslash (\) to signify the following character has been escaped - something which is done for multiple purposes. Previously we saw how \s could be used to specify white-space, similarly other characters can be used to specify other characters.

\s
Whitespace (such as the character produced by pressing the spacebar)
\S
Non-whitespace
\d
Matches a digit (0-9)
\D
Matches anything that isn't a digit
\x
Matches a hexadecimal number
\O
Matches an octal digit
\w
Matches a word character (any alphanumeric character or an underscore)
\W
Matches any non-word character
\c
Matches a control character

Similar to the above escaped characters that are used for ranges of characters (of a type), there are other special characters that are used to symbolise other things such as a newline.

\n
Matches a new line
\t
Matches a tabstop
\r
Matches a carriage return
\v
Matches a vertical tab character
\xxx
Matches the octal digit xxx
\xhh
Matches the hexadecimal digit hh
\f
Matches a form feed

The following characters must be escaped when used: ^, (, ), , ., *, [, ], {, }, \, |, $, ?, +