Regular expressions are incredibly powerful but notoriously difficult to remember. Even experienced developers need to look up syntax frequently. This comprehensive cheat sheet covers all the most important regex patterns, from basic character classes to advanced lookahead assertions. Keep this guide open while learning regex, bookmark it for quick reference, and use it with ToolPilot's regex tester for hands-on practice.
Ad space - Advertisement placement 1
Character Classes
Pattern
Meaning
Example
.
Any character except newline
a.c matches abc, adc, a1c
\d
Digit (0-9)
\d{3} matches 123
\D
Non-digit
\D+ matches abc
\w
Word character [a-zA-Z0-9_]
\w+ matches hello_world
\W
Non-word character
\W matches !, @, #
\s
Whitespace
\s+ matches spaces, tabs
\S
Non-whitespace
\S+ matches hello
[abc]
Character set
[abc] matches a, b, or c
[^abc]
Negated set
[^abc] matches any char except a, b, c
[a-z]
Range
[a-z] matches lowercase letters
Quantifiers
Pattern
Meaning
Example
*
Zero or more
a*b matches b, ab, aab
+
One or more
a+b matches ab, aab (not b)
?
Zero or one (optional)
a?b matches b, ab
{n}
Exactly n times
a{3} matches aaa
{n,}
n or more times
a{2,} matches aa, aaa
{n,m}
Between n and m times
a{2,4} matches aa, aaa, aaaa
Anchors and Assertions
Pattern
Meaning
Example
^
Start of string/line
^hello matches hello at start
$
End of string/line
world$ matches world at end
\b
Word boundary
\bword\b matches word (not sword)
\B
Non-word boundary
\Bword matches sword
Ad space - Advertisement placement 2
Groups and Capture
Pattern
Meaning
Example
(abc)
Capture group
(\d{3}) captures 123
(?:abc)
Non-capturing group
(?:foo|bar) without capture
(?=...)
Positive lookahead
foo(?=bar) matches foo only if followed by bar
(?!...)
Negative lookahead
foo(?!bar) matches foo not followed by bar
(?<=...)
Positive lookbehind
(?<=foo)bar matches bar only if preceded by foo
(?<!...)
Negative lookbehind
(?<!foo)bar matches bar not preceded by foo
Alternation
Use the pipe symbol (|) for alternation—matching one of several options. The pattern cat|dog matches either cat or dog. In groups: (foo|bar|baz) matches any of the three options. Alternation is tried left-to-right, so order matters in some cases.
cat|dog matches "cat" or "dog"
(jpg|png|gif) matches any image format
colou?r matches "color" or "colour"
Common Patterns
Purpose
Pattern
Email
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
URL
https?://[^\s]+
Phone (US)
\d{3}-\d{3}-\d{4}
IP Address
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
Hex Color
#[0-9a-fA-F]{6}
Date (YYYY-MM-DD)
\d{4}-\d{2}-\d{2}
Positive Integer
[1-9]\d*
Negative Integer
-[1-9]\d*
Flags
Regex flags modify matching behavior. The 'i' flag enables case-insensitive matching so /hello/i matches HELLO. The 'g' flag enables global matching, finding all matches instead of just the first. The 'm' flag enables multiline mode where ^ and $ match line boundaries not just string boundaries. The 's' flag makes . match newlines. Different languages support different flags.
Test Regex Patterns Instantly
Use ToolPilot's regex tester to experiment with patterns and see instant feedback.
Both match digits, but \d is more portable across different regex engines. In JavaScript and most modern languages, they're equivalent. However, in some regex flavors, \d includes Unicode digit characters while [0-9] matches only ASCII digits 0-9. For portability, use [0-9] when you specifically want ASCII digits.
When should I use (group) vs (?:group)?
Use capturing groups (group) when you need to extract the matched content. Use non-capturing groups (?:group) when you just need grouping for alternation or quantifiers without extracting the result. Non-capturing groups are slightly more efficient since the regex engine doesn't store the match.
How do I match a literal period or question mark?
Escape special characters with a backslash. \. matches a literal period. \? matches a literal question mark. \* matches a literal asterisk. Any special character can be matched literally by preceding it with a backslash. In character classes [.?*], special meaning is removed so escaping isn't needed.
What's the difference between * and *??div>
Both match zero or more occurrences, but they differ in greediness. * is greedy—it matches as much as possible. *? is lazy—it matches as little as possible. For example, a.*b on "axxxb" with greedy * matches the entire string. With lazy *?, it matches just "axxxb" but stops at the first b. Lazy quantifiers are useful when matching between delimiters.