Menu

Regex Cheat Sheet

Last updated

Character classes

Match a single character from a set.

TokenMatches
.Any character except a newline
\dAny digit (0-9)
\DAny non-digit
\wWord character (letter, digit, underscore)
\WAny non-word character
\sAny whitespace (space, tab, newline)
[abc]Any one of a, b, or c
[a-z]Any lowercase letter in the range
[^abc]Any character except a, b, or c

Quantifiers

Specify how many times the preceding token repeats.

TokenMatches
*Zero or more
+One or more
?Zero or one (optional)
{3}Exactly 3 times
{2,4}Between 2 and 4 times
{2,}2 or more times
*?Lazy: as few as possible
+?Lazy: one or more, minimal

Anchors & boundaries

Match positions rather than characters.

TokenMatches
^Start of the string (or line)
$End of the string (or line)
\bA word boundary
\BA non-word boundary
^abc$A string that is exactly abc
\bword\bword as a whole word

Groups & capturing

Group tokens and capture what they match.

TokenMatches
(abc)Capturing group, stored as group 1
(?:abc)Non-capturing group
(?<year>\d{4})Named capturing group year
(ab)+Repeat the whole group
\1Backreference to group 1
\k<year>Backreference to a named group

Alternation

Match one pattern or another.

TokenMatches
a|bEither a or b
cat|dogThe word cat or dog
(jpg|png|gif)Any one of the alternatives, captured
gr(a|e)ygray or grey
^(yes|no)$Exactly yes or exactly no

Lookarounds

Assert what comes before or after without consuming it.

TokenMatches
(?=abc)Lookahead: followed by abc
(?!abc)Negative lookahead: not followed by abc
(?<=abc)Lookbehind: preceded by abc
(?<!abc)Negative lookbehind: not preceded by abc
\d+(?= dollars)Digits only when followed by dollars
(?<=\$)\d+Digits only when preceded by $

Flags

Modify how the whole pattern is applied.

FlagWhat it does
gGlobal: find all matches, not just the first
iCase-insensitive matching
mMultiline: ^ and $ match line ends
sDotall: . also matches newlines
uUnicode mode
xExtended: ignore whitespace in the pattern

Common patterns

Ready-to-adapt patterns for everyday validation.

PatternMatches
^\d+$A whole number (digits only)
^[\w.+-]+@[\w-]+\.[\w.-]+$A basic email address
https?:\/\/[^\s]+An http or https URL
^\d{4}-\d{2}-\d{2}$A date like 2026-05-27
^\d{3}-\d{3}-\d{4}$A US phone number
^#?[0-9a-fA-F]{6}$A 6-digit hex color
\s+One or more whitespace characters

Every regex token you reach for, on one page. This regex cheat sheet is a quick reference for building regular expressions - character classes, quantifiers, anchors, groups, alternation, lookarounds, and flags.

The syntax here follows the common PCRE / JavaScript flavor used by most languages. Copy a pattern, or try it live in the regex tester tool to see exactly what it matches, with the JS playground a click away.

Regex cheat sheet FAQ

Is this regex cheat sheet free?
Yes. This regex cheat sheet is completely free, with no sign-up required. Bookmark it and come back whenever you need to look up a token, quantifier, or flag.
What is the difference between greedy and lazy quantifiers?
Greedy quantifiers like * and + match as much as possible, then give characters back if the rest of the pattern needs them. Lazy quantifiers, written by adding ? (such as *? or +?), match as little as possible and only expand when forced. For example, on <a><b> the pattern <.+> greedily matches the whole string, while <.+?> matches just <a>.
What is a capture group in regex?
A capture group is a part of a pattern wrapped in parentheses, like (\d{4}), whose matched text is saved and numbered so you can reuse or extract it. You reference it later with a backreference such as \1, or name it with (?<year>\d{4}) for clarity. If you want to group tokens without capturing, use a non-capturing group (?:...).
Can I practice regex online?
Yes. Open the regex tester tool to type a pattern and see exactly what it matches against your own text, live in the browser. You can also drop any pattern into the JS playground to use it in code. When you want structure, Coddy's free interactive course covers the building blocks step by step.
Is this cheat sheet good for beginners?
Yes. It is organized from character classes and quantifiers (the foundations) down to lookarounds and ready-made patterns, so you can start matching simple text right away and grow into advanced expressions.
Coddy programming languages illustration

Learn Regex with Coddy

GET STARTED