Regex Cheat Sheet with Live Testing — 2026 Guide
What Is a Regular Expression?
A regular expression (regex) is a sequence of characters that defines a search pattern. Regex engines scan a string and report whether the pattern matches, where it matches, or what it captures. They are built into virtually every programming language — JavaScript, Python, Java, Go, Rust — and into most text editors, command-line tools, and databases.
The power of regex lies in its compactness. A single pattern can replace dozens of lines of string-parsing logic. The trade-off is readability: regex syntax is dense, and even experienced developers reach for a reference when writing complex patterns.
Test Regex Patterns Instantly
Write your pattern, paste your test string, and see matches highlighted in real time — no install, no account.
Open Regex Tester →Quick-Reference Cheat Sheet
Character Classes
| Syntax | Matches | Example |
|---|---|---|
. | Any character except newline | c.t → "cat", "cut", "c3t" |
\d | Any digit (0–9) | \d\d\d → "042", "867" |
\D | Any non-digit | \D+ → "hello", "abc" |
\w | Word character (a–z, A–Z, 0–9, _) | \w+ → "username_1" |
\W | Non-word character | \W → " ", "\!", "@" |
\s | Whitespace (space, tab, newline) | \s+ → " " |
\S | Non-whitespace | \S+ → "hello" |
[abc] | Any of a, b, or c | [aeiou] → vowels |
[^abc] | Any character NOT a, b, or c | [^0-9] → non-digits |
[a-z] | Character in range a–z | [a-zA-Z] → any letter |
Quantifiers
| Syntax | Meaning | Example |
|---|---|---|
* | 0 or more (greedy) | a* → "", "a", "aaa" |
+ | 1 or more (greedy) | a+ → "a", "aaa" |
? | 0 or 1 (optional) | colou?r → "color", "colour" |
{n} | Exactly n times | \d{4} → "2026" |
{n,} | n or more times | \w{3,} → 3+ word chars |
{n,m} | Between n and m times | \d{2,4} → 2–4 digits |
*? | 0 or more (lazy) | Stops at first match |
+? | 1 or more (lazy) | Stops at first match |
Anchors and Boundaries
| Syntax | Meaning |
|---|---|
^ | Start of string (or line in multiline mode) |
$ | End of string (or line in multiline mode) |
\b | Word boundary |
\B | Non-word boundary |
Groups and Alternation
| Syntax | Meaning | Example |
|---|---|---|
(abc) | Capturing group | (\d{4})-(\d{2}) captures year and month |
(?:abc) | Non-capturing group | Groups without capturing overhead |
(?<name>abc) | Named capture group | (?<year>\d{4}) |
a|b | Alternation — a or b | cat|dog matches "cat" or "dog" |
Lookaheads and Lookbehinds
| Syntax | Type | Meaning |
|---|---|---|
(?=abc) | Positive lookahead | Matches if followed by "abc" |
(?\!abc) | Negative lookahead | Matches if NOT followed by "abc" |
(?<=abc) | Positive lookbehind | Matches if preceded by "abc" |
(?<\!abc) | Negative lookbehind | Matches if NOT preceded by "abc" |
Common Regex Patterns
Email Address
^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$
Matches standard email formats. Note: full RFC 5322 compliance requires a far more complex pattern — this covers the overwhelming majority of real-world addresses.
US Phone Number
^\+?1?\s?(\(\d{3}\)|\d{3})[\s.\-]?\d{3}[\s.\-]?\d{4}$
Matches formats like (555) 867-5309, 555.867.5309, and +15558675309.
URL
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)
Matches HTTP and HTTPS URLs. For production validation, consider using the URL constructor in JavaScript instead — it handles edge cases regex cannot.
IPv4 Address
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Hex Color Code
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Regex Flags
Flags modify how the regex engine interprets the pattern:
| Flag | Name | Effect |
|---|---|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | Treats uppercase and lowercase as equal |
m | Multiline | ^ and $ match line boundaries, not just string boundaries |
s | Dotall | Makes . match newlines as well |
u | Unicode | Enables full Unicode matching |
Debugging Tips
1. Start Small and Build Up
Don't write a 100-character regex in one shot. Start with the simplest possible pattern that matches part of your target, verify it, then extend it. A live tester like the one below makes this workflow fast.
2. Use Non-Greedy Quantifiers When Matching Between Delimiters
If you want to match content between HTML tags, <.*> will grab everything from the first opening tag to the last closing tag on the line. Use <.*?> instead — the lazy quantifier stops at the first closing angle bracket.
3. Escape Special Characters in Literals
The characters . * + ? ^ $ { } [ ] ( ) | \ are all special in regex. If you want to match a literal period, write \.. Forgetting this is the source of many regex bugs.
4. Test Against Both Matching and Non-Matching Cases
A regex that matches valid emails is useless if it also matches invalid strings like @.com or user@. Always test your pattern against strings it should reject, not just strings it should accept.
5. Watch for Catastrophic Backtracking
Patterns like (a+)+ against a non-matching string can cause exponential backtracking, hanging your application. If your regex uses nested quantifiers on overlapping character classes, test it against adversarial input.
Try It Live
Use our free regex tester to validate patterns against real input — with syntax highlighting and instant match feedback.
Open Regex Tester →