Regular Expressions Cheatsheet

. (Dot)   The wildcard, The Joker. The dot is a single instance of any possible character.


^ Start of string
A Start of string
$ End of string
Z End of string
b Word boundary
B Not word boundary
< Start of word
> End of word
c Control Character
s White Space
S Not white Space
d Digit
D Not digit
w Word
W Not word
x Hexadecimal digit
O Octal digit
?= Lookahead assertion
?! Negative lookahead
?<= Lookbehind assertion
?!= or also ? Negative lookbehind
?> Once-only Subexpression
?() Condition [If Then]
?()| Condition [If Then Else]
?# Comment
* 0 or more occurences
+ 1 or more occurences
? 0 or 1 occurences
{5} Exactly 5 occurences (or any other number instead of 5)
{5,} 5 or more occurences
{5,8} 5, 6, 7 or 8 occurences
x? Ungreedy version of “x”, where x is a quantifier (see before)
/ Escape one character
Note that all of these characters need to be escaped (see escape character above)
^ $ ( ) < >
[ { | . *
+ ?
g Global match
i Make case-insensitive
m Match multiple lines
s Treat a string as a single line
x Allow the occurence of whitespace and comments in the string
e Evaluate the replacement
U Use an Ungreedy pattern
n Newline
r Carriage return
t Tab
v Vertical tab
f Form feed
xxx Octal character xxx
xhh Hex character hh
. Matches all characters except newline
(x|y) Matches x or y
(…) A group, matches the group exactly (xyz) matches ‘xyz’
(?:…) Lazy group, matches every occurence of group, not only the first and the last (as greedy does)
[xyz] Range, matches x, y or z
[^xyz] Negative range, matches NOT x, y or z
[a-z] Matches any letter between a and z
[A-Z] Matches any upper case letter between a and z
[0-9] Matches any number between 0 and 9 (digit)
x Matches the “x”-th occurence of the pattern
$x Replaces “x”-th non-passive group that matches
$2 Replaces “def” in /^(abc(def))$/
$1 Replaces “def” in /^(?:abc(def))$/
$` Replaces before the matched string
$’ Replaces after the matched string
$+ Replaces the last matched string
$& Replaces the entire matched string

Leave a Comment

%d bloggers like this: