Regex Cheat Sheet for Developers — With Live Tester (2026)
Regular expressions are one of the most useful and most avoided tools in a developer's toolkit. This cheat sheet gives you every pattern you'll actually use, plus real examples you can test immediately.
Ram

Regular expressions (regex) are simultaneously one of the most useful and most dreaded tools in programming. When you know them, you can accomplish in one line what would otherwise take 20 lines of string parsing code. When you don't, you write brittle string manipulation that breaks on edge cases.
Test any pattern from this guide instantly with our Regex Tester — real-time match highlighting, no page reload.
Regex Fundamentals
What Is a Regular Expression?
A regular expression is a sequence of characters that defines a search pattern. They're used for:
- Validation — Is this a valid email? Phone number? URL?
- Extraction — Pull out all dates from a log file
- Replacement — Replace all occurrences of a pattern
- Splitting — Split a string on multiple delimiters
The Two Parts of a Regex
A complete regex has the pattern and flags:
/pattern/flags Pattern: /\d{3}-\d{4}/ With flags: /\d{3}-\d{4}/gi
Character Classes (What to Match)
| Pattern | Matches | Example |
|---|---|---|
. | Any character except newline | c.t matches cat, cut, c3t |
\d | Any digit [0-9] | \d\d matches 42 |
\D | Any non-digit | \D+ matches hello |
\w | Word char [a-zA-Z0-9_] | \w+ matches hello_world |
\W | Non-word char | \W matches space, comma |
\s | Whitespace (space, tab, newline) | \s+ matches spaces |
\S | Non-whitespace | \S+ matches words |
[abc] | Any of a, b, c | [aeiou] matches vowels |
[^abc] | Not a, b, or c | [^0-9] matches non-digits |
[a-z] | Range a through z | [A-Z] matches uppercase |
Anchors (Where to Match)
| Pattern | Matches | Example |
|---|---|---|
^ | Start of string/line | ^Hello only at start |
$ | End of string/line | world$ only at end |
\b | Word boundary | \bcat\b matches cat not catch |
\B | Non-word boundary | \Bcat matches in scat |
Quantifiers (How Many to Match)
| Pattern | Matches | Example |
|---|---|---|
| 0 or more | abc matches ac, abc, abbc |
+ | 1 or more | ab+c matches abc, abbc (not ac) |
? | 0 or 1 (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 | \d{2,4} matches 2 to 4 digits |
By default, quantifiers are greedy — they match as much as possible. Add ? after to make them lazy — match as little as possible.
const text = "<b>bold</b> and <i>italic</i>";
// Greedy: matches everything from first < to last > /<.+>/.exec(text); // "<b>bold</b> and <i>italic</i>"
// Lazy: matches each tag separately /<.+?>/.exec(text); // "<b>"
Groups and Alternation
| Pattern | Meaning | Example | ||
|---|---|---|---|---|
(abc) | Capture group | (hello) captures hello | ||
(?:abc) | Non-capture group | (?:http\ | https) — group without capturing | |
(?<name>abc) | Named capture group | (?<year>\d{4}) captures as year | ||
a\ | b | Alternation (OR) | cat\ | dog matches either |
\1 | Backreference to group 1 | (\w+)\s\1 matches repeated word |
Flags
| Flag | Meaning |
|---|---|
g | Global — find all matches, not just first |
i | Case-insensitive |
m | Multiline — ^ and $ match line start/end |
s | Dotall — . matches newlines too |
u | Unicode — enables full Unicode matching |
20+ Real-World Regex Patterns
Test all of these in our Regex Tester:
Validation Patterns
Email (common use)^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Indian Mobile Number
^[6-9]\d{9}$
Indian PAN Card
^[A-Z]{5}[0-9]{4}[A-Z]$
Indian PIN Code
^[1-9][0-9]{5}$
URL (http/https)
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,6}(\/[-a-zA-Z0-9@:%_+.~#?&/=]*)?$
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])$
Time (HH:MM or HH:MM:SS)
^([01]\d|2[0-3]):([0-5]\d)(:([0-5]\d))?$
Strong Password (8+ chars, uppercase, lowercase, digit, special)
^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[@$!%?&])[A-Za-z\d@$!%?&]{8,}$
Credit Card Number (basic)
^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$
Extraction Patterns
Extract Hashtags from Text#\w+
Extract @Mentions
@\w+
Extract All URLs from Text
https?:\/\/[^\s]+
Extract Numbers (including decimals)
-?\d+(\.\d+)?
Extract Content Between HTML Tags
<[^>]+>(.*?)<\/[^>]+>
Extract File Extension
\.([a-zA-Z0-9]+)$
Find and Replace Patterns
Remove HTML Tags<[^>]*>
Replace with: (empty string)
Remove Extra Whitespace
\s{2,}
Replace with: single space
Convert Kebab-Case to camelCase (in 2 steps)
-([a-z])
Replace with: uppercase of group 1
Remove All Non-Alphanumeric Characters
[^a-zA-Z0-9]
Replace with: (empty string)
Normalize Line Endings (CRLF to LF)
\r\n
Replace with: \n
Regex in JavaScript
// Test: does string match?
/^\d+$/.test("12345"); // true
/^\d+$/.test("123a"); // false
// Find first match "hello world".match(/\w+/); // ["hello"]
// Find all matches (g flag) "hello world".match(/\w+/g); // ["hello", "world"]
// Replace "foo bar".replace(/\s+/, "_"); // "foo_bar" (first only) "foo bar baz".replaceAll(/\s+/g, "_"); // "foo_bar_baz"
// Split "one,two,,three".split(/,+/); // ["one", "two", "three"]
// Named capture groups const { year, month, day } = "2026-04-29".match( /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/ )?.groups ?? {};
Regex in Python
import re
Test match
bool(re.match(r'^\d+$', '12345')) # True
Find all
re.findall(r'\w+', 'hello world') # ['hello', 'world']
Replace
re.sub(r'\s+', '_', 'foo bar baz') # 'foo_bar_baz'
Named groups
match = re.match(r'(?P<year>\d{4})-(?P<month>\d{2})', '2026-04')
match.group('year') # '2026'
Common Regex Pitfalls
Catastrophic Backtracking Patterns like(a+)+$ applied to a long string of as followed by a non-matching character cause exponential backtracking, making the engine hang. Avoid nested quantifiers on the same character class.
Matching Across Lines
By default, . doesn't match newlines. Use the s flag (dotall) or [\s\S] to match any character including newlines.
Unicode Characters
In JavaScript, \d only matches [0-9]. It doesn't match Arabic-Indic numerals or other Unicode digits. Use the u flag and \p{Decimal_Number} for proper Unicode digit matching.
Anchoring Validation Patterns
Forgetting ^ and $ anchors means test@email.com,injection would pass email validation because the regex matches the email portion, not the whole string.
Use our Regex Tester to test every pattern in this guide against your own input data, with real-time match highlighting and flag support.
