Useful regular expressions cheat sheet

Regular expressions are really handy if we can use them correctly. It can reduce a lot of code and make the code smarter. The regular expression is nothing but a pattern matching inside a string. Following are some really useful regular expression cheat sheet that every developer should know:


Start of string, or start of line in multi-line pattern
Start of string
End of string, or end of line in multi-line pattern
End of string
Word boundary
Not word boundary
Start of word
End of word

Character Classes

Control character
White space
Not white space
Not digit
Not word
Hexade­cimal digit
Octal digit


0 or more
Exactly 3
1 or more
3 or more
0 or 1
3, 4 or 5

Groups and Ranges

Any character except new line (\n)
a or b
Passive (non-c­apt­uring) group
Range (a or b or c)
Not (a or b or c)
Lower case letter from a to q
Upper case letter from A to Q
Digit from 0 to 7
Group/­sub­pattern number “­x”

Escape Sequences

Escape following character
Begin literal sequence
End literal sequence


Upper case letters
Lower case letters
All letters
Digits and letters
Hexade­cimal digits
Space and tab
Blank characters
Control characters
Printed characters
Printed characters and spaces
Digits, letters and underscore

Pattern Modifiers

Global match
i *
m *
Multiple lines
s *
Treat string as single line
x *
Allow comments and whitespace in pattern
e *
Evaluate replac­ement
U *
Ungreedy pattern

String Replac­ement

nth non-pa­ssive group
“­xyz­” in /^(abc­(xy­z))$/
“­xyz­” in /^(?:a­bc)­(xyz)$/
Before matched string
After matched string
Last matched string
Entire matched string


Lookahead assertion
Negative lookahead
Lookbehind assertion
?!= or ?<!
Negative lookbehind
Once-only Subexp­ression
Condition [if then]
Condition [if then else]

Common Metach­ara­cters


Special Characters

New line
Carriage return
Vertical tab
Form feed
Octal character xxx
Hex character hh