ITT
All Cheatsheets

Regex Cheatsheet — Complete Regular Expressions Reference

A complete, printable regex reference guide. Covers anchors, character classes, quantifiers, groups, lookaheads, and more. Works for JavaScript, Python, and most regex engines.

regex cheatsheet
regular expression reference
regex syntax guide
regex quick reference
regex symbols list

Anchors

Syntax / PatternMeaningExample / Notes
^Start of string (or line with m flag)/^hello/ matches "hello world" but not "say hello"
$End of string (or line with m flag)/world$/ matches "hello world" but not "world hello"
\bWord boundary/\bcat\b/ matches "cat" but not "catch"
\BNot a word boundary/\Bcat/ matches "concatenate" but not "cat"

Character Classes

Syntax / PatternMeaningExample / Notes
.Any character except newlineWith the s (dotAll) flag, also matches newlines
\dDigit [0-9]\D matches non-digit
\wWord character [a-zA-Z0-9_]\W matches non-word character
\sWhitespace (space, tab, newline)\S matches non-whitespace
[abc]Character set: a, b, or c[^abc] negates — matches anything except a, b, c
[a-z]Range: any lowercase letter[a-zA-Z0-9] alphanumeric

Quantifiers

Syntax / PatternMeaningExample / Notes
*Zero or more (greedy)a* matches "", "a", "aaa"
+One or more (greedy)a+ matches "a", "aaa" but not ""
?Zero or one (optional)colou?r matches "color" and "colour"
{n}Exactly n times\d{4} matches exactly 4 digits
{n,}n or more times\d{2,} matches 2+ digits
{n,m}Between n and m times\d{2,4} matches 2, 3, or 4 digits
*? +? ??Lazy (non-greedy) versions<.+?> matches the shortest possible tag

Groups & References

Syntax / PatternMeaningExample / Notes
(abc)Capturing groupCaptured in $1, $2... or match[1], match[2]
(?:abc)Non-capturing groupGroups without capturing — better for performance
(?<name>abc)Named capture group (JS/Python)Access via match.groups.name or (?P<name>...) in Python
\1Backreference to group 1(\w+) \1 matches repeated words like "hello hello"

Lookaheads & Lookbehinds

Syntax / PatternMeaningExample / Notes
(?=abc)Positive lookahead — followed by abc\d+(?= dollars) matches numbers before " dollars"
(?!abc)Negative lookahead — NOT followed by abc\d+(?! dollars) matches numbers not before " dollars"
(?<=abc)Positive lookbehind — preceded by abc(?<=\$)\d+ matches digits after a dollar sign
(?<!abc)Negative lookbehindNot supported in all engines

Flags

Syntax / PatternMeaningExample / Notes
gGlobal — find all matchesWithout g, only the first match is returned
iCase-insensitive/hello/i matches "Hello", "HELLO", "hello"
mMultiline — ^ and $ match line start/endAffects behavior of ^ and $ anchors
sDotAll — . matches newlines tooES2018+ / Python re.DOTALL
uUnicode modeRequired for Unicode property escapes like \p{L}
dIndices — include match start/end positionsES2022+ only

Common Patterns

Syntax / PatternMeaningExample / Notes
Email/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
URL/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,6}/
IP Address/\b(?:\d{1,3}\.){3}\d{1,3}\b/
US Phone/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/
Date YYYY-MM-DD/^\d{4}-\d{2}-\d{2}$/
Hex Color/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/
Slug/^[a-z0-9]+(?:-[a-z0-9]+)*$/
Password (8+ chars, 1 letter, 1 digit)/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/

Frequently Asked Questions

What is the difference between * and + in regex?

`*` means zero or more occurrences — the match succeeds even if the character is absent. `+` means one or more — at least one occurrence is required.

How do I make a regex case-insensitive?

Add the `i` flag: `/pattern/i` in JavaScript, or `re.compile("pattern", re.IGNORECASE)` in Python.

What does \b do in regex?

`\b` is a word boundary assertion. It matches the position between a word character (\w) and a non-word character. `/\bcat\b/` matches "cat" in "the cat sat" but not in "catch".

Ready to put this to use?

Test patterns interactively in our live tool.

Open Regex Tester

More Cheatsheets