Show Menu
Cheatography

RegExp Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Character Classes

[abc]
any of these characters (a or b or c)
[^abc]
not any of these characters (not a or b or c)
[a-c]
any characters in the range (a through c)
[^a-c]
not any characters in this range (not a through c)
(X|Y)
X or Y

Predefined Character Classes

.
any character
\d
a digit: [0-9]
\D
a non-digit: [^0-9]
\s
a whitespace character: " " (space), \r (the carriage return), \n (newline), \t (tab), and \f (the form feed)
\S
a non-wh­ite­space character: [^\s]
\w
a word character: [a-zA-­Z0-9_]
\W
a non-word character: [^\w]

Boundaries

^
the beginning of the line
$
the end of the line
\b
a word boundary
\B
a non-word boundary
boundaries - do not select any characters but sets pattern to some edge
 

Quanti­fiers

X*
0 or more repiti­tions of X
X+
1 or more repiti­tions of X
X?
0 or 1 instance of X
X{m}
exactly m instances of X
X{m, }
at least m instances of X
X{m,n}
between m and n (inclu­sive) instances of X
use ( . . . ) parent­hesis to group things you want to find repeti­tions of

Groups & References

( . . . )
define­s/c­aptures a group
\1
contents of group 1
\2
contents of group 2
$1
replace with group 1
$2
replace with group 2
ex:
.replace( / regExp /, $2)

{replaces regular expres­sions with group 2}

Metach­ara­cters

(
)
^
$
[
]
.
\
{
}
*
|
<
>
+
?
special characters to be escaped with \