Regex Cheat Sheet 2026: The Complete Regular Expression Reference
๐ Table of Contents
Regular Expressions (regex) are powerful patterns used for matching, searching, and manipulating text. Every developer needs regex skills โ from form validation to log parsing to data extraction. This complete cheat sheet contains everything you need to master regex in 2026.
Whether you're working in JavaScript, Python, Java, PHP, or any other language, the core regex syntax remains the same. Use this reference guide whenever you need to write or debug a regex pattern.
Basic Syntax
Start with the fundamentals. In regex, most characters match themselves literally.
| Pattern | Description | Example |
|---|---|---|
abc |
Matches exact characters "abc" | "abc" in "abcdef" |
123 |
Matches exact digits "123" | "123" in "test123" |
hello world |
Matches exact phrase with space | "hello world" |
Metacharacters (Special Characters)
These characters have special meanings in regex. To match them literally, escape with backslash
\.
| Character | Meaning | Example |
|---|---|---|
. |
Any character except newline | a.c โ "abc", "a1c", "a-c" |
^ |
Start of string | ^Hello โ "Hello World" |
$ |
End of string | World$ โ "Hello World" |
* |
Zero or more times | ab*c โ "ac", "abc", "abbc" |
+ |
One or more times | ab+c โ "abc", "abbc" (not "ac") |
? |
Zero or one time (optional) | colou?r โ "color", "colour" |
| |
OR operator (alternation) | cat|dog โ "cat" or "dog" |
\ |
Escape special character | \. โ literal "." |
Remember to Escape!
To match literal . * + ? $
^ | \ [ ] (
) { }, prefix with backslash: \.
\* etc.
Character Classes
Define sets of characters to match. Square brackets create custom classes.
| Pattern | Meaning | Equivalent |
|---|---|---|
[abc] |
Any one of a, b, or c | a|b|c |
[^abc] |
NOT a, b, or c | Any except a, b, c |
[a-z] |
Any lowercase letter | a through z |
[A-Z] |
Any uppercase letter | A through Z |
[0-9] |
Any digit | 0 through 9 |
[a-zA-Z] |
Any letter (case insensitive) | a-z or A-Z |
[a-zA-Z0-9] |
Any alphanumeric | Letters or digits |
Shorthand Character Classes
| Shorthand | Meaning | Equivalent |
|---|---|---|
\d |
Any digit | [0-9] |
\D |
Any non-digit | [^0-9] |
\w |
Word character | [a-zA-Z0-9_] |
\W |
Non-word character | [^a-zA-Z0-9_] |
\s |
Whitespace | [ \t\n\r\f\v] |
\S |
Non-whitespace | [^ \t\n\r\f\v] |
Quantifiers
Control how many times a pattern should match.
| Quantifier | Meaning | Example |
|---|---|---|
* |
0 or more | a* โ "", "a", "aaa" |
+ |
1 or more | a+ โ "a", "aaa" (not "") |
? |
0 or 1 (optional) | a? โ "", "a" |
{n} |
Exactly n times | a{3} โ "aaa" |
{n,} |
n or more times | a{2,} โ "aa", "aaa", "aaaa" |
{n,m} |
Between n and m times | a{2,4} โ "aa", "aaa", "aaaa" |
Greedy vs Lazy Quantifiers
| Greedy | Lazy | Behavior |
|---|---|---|
* |
*? |
Match as few as possible |
+ |
+? |
Match minimum needed |
? |
?? |
Prefer 0 over 1 |
{n,m} |
{n,m}? |
Match n (minimum) |
Anchors & Boundaries
Anchors don't match characters โ they match positions.
| Anchor | Meaning | Example |
|---|---|---|
^ |
Start of string/line | ^Start โ "Start here" |
$ |
End of string/line | end$ โ "The end" |
\b |
Word boundary | \bword\b โ whole word only |
\B |
Non-word boundary | \Bword โ "sword" (not "word") |
Groups & Capturing
Parentheses create groups for capturing or applying quantifiers.
| Pattern | Meaning | Usage |
|---|---|---|
(abc) |
Capturing group | Captures "abc" as $1 |
(?:abc) |
Non-capturing group | Groups but doesn't capture |
(?<name>abc) |
Named capturing group | Captures as "name" |
\1, \2 |
Backreference | Match same text again |
$1, $2 |
Replacement reference | Use captured text in replace |
Example: Swap First & Last Name
Pattern: (\w+)\s(\w+)
Replace: $2, $1
"John Smith" โ "Smith, John"
Lookahead & Lookbehind
Assert patterns without including them in the match.
| Pattern | Name | Meaning |
|---|---|---|
(?=...) |
Positive Lookahead | Must be followed by ... |
(?!...) |
Negative Lookahead | Must NOT be followed by ... |
(?<=...) |
Positive Lookbehind | Must be preceded by ... |
(? |
Negative Lookbehind | Must NOT be preceded by ... |
Regex Flags
Flags modify how the pattern matching works.
| Flag | Name | Description |
|---|---|---|
g |
Global | Find all matches, not just first |
i |
Case Insensitive | /hello/i matches "HELLO" |
m |
Multiline | ^ and $ match line start/end |
s |
Dotall | . matches newlines too |
u |
Unicode | Enable full Unicode support |
Common Regex Patterns
Copy-paste these tested patterns for common use cases:
| Use Case | Pattern |
|---|---|
^[\w.-]+@[\w.-]+\.\w{2,}$ |
|
| URL | https?:\/\/[\w.-]+(?:\/[\w.-]*)*\/? |
| Phone (US) | \(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4} |
| Date (YYYY-MM-DD) | \d{4}-\d{2}-\d{2} |
| IP Address | \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b |
| HTML Tag | <[^>]+> |
| Hex Color | #?([a-fA-F0-9]{6}|[a-fA-F0-9]{3}) |
| Username | ^[a-zA-Z][a-zA-Z0-9_]{2,15}$ |
| Strong Password | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$ |
| Credit Card | \b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b |
๐งช Test Your Regex Patterns
Use our free Regex Tester to validate patterns in real-time with instant feedback and match highlighting.
Open Regex Tester โFrequently Asked Questions
What is regex used for?
Regular expressions are used for: Form validation (emails, phones, passwords), Search and replace in text editors, Data extraction from strings, Log file parsing, URL routing in web frameworks, Input sanitization, and Text processing in ETL pipelines.
What's the difference between * and + in regex?
* (asterisk) matches zero or more occurrences โ the pattern is
optional and can repeat. + (plus) matches one or more
occurrences โ at least one match is required. Example: a* matches "", "a", "aa"
but a+ only matches "a", "aa" (not empty string).
How do I match any character including newlines?
The dot . matches any character except newlines by default. To
match truly any character including newlines, use [\s\S] (whitespace or
non-whitespace = everything), or enable the s (dotall/single-line) flag:
/pattern/s.
What does the ^ symbol mean inside [^...]?
The caret ^ has two different meanings: At the start of a
pattern, it means "beginning of string". Inside a character
class [^...], it means "NOT these characters" โ a negated
character class. [^abc] matches any character except a, b, or c.
How do I make a regex case insensitive?
Add the i flag to your pattern. In JavaScript: /pattern/i. In
Python: re.compile(pattern, re.IGNORECASE) or re.I. In PHP:
/pattern/i. The pattern will then match both uppercase and lowercase
variations.
What's the difference between capturing and non-capturing groups?
Capturing groups (pattern) save the matched text for later use
via backreferences ($1, $2) or match arrays. Non-capturing groups
(?:pattern) group patterns without saving โ useful when you need grouping for
quantifiers but don't need the captured value. Non-capturing is slightly faster.
Why is my regex matching too much (greedy matching)?
Quantifiers like * and + are greedy by default โ
they match as much as possible. To make them lazy (match minimum), add
? after: *? +? ??. Example: for
"<div>text</div>", pattern <.*> matches the whole thing, but
<.*?> matches just "<div>".
Code Formatter ยฉ 2026. Professional developer tools built with privacy and performance in mind.