Regex Cheat Sheet: Essential Patterns Every Developer Should Know
· Cosyslabs
Regular expressions match text patterns using character classes, quantifiers, anchors, and groups. This reference covers the 20 most important regex constructs with examples in JavaScript, Python, and the most common use cases including email validation, URL parsing, phone numbers, and date extraction.
Quick Reference Table
| Syntax | Meaning | Example |
|---|---|---|
. | Any character except newline | a.c matches abc, a1c |
\d | Digit [0-9] | \d{4} matches 2026 |
\w | Word char [a-zA-Z0-9_] | \w+ matches hello_123 |
\s | Whitespace | \s+ matches spaces/tabs |
^ | Start of string | ^Hello |
$ | End of string | world$ |
* | 0 or more | a* matches ``, a, aaa |
+ | 1 or more | a+ matches a, aaa |
? | 0 or 1 (optional) | colou?r matches color/colour |
{n} | Exactly n | \d{4} |
{n,m} | Between n and m | \d{2,4} |
[abc] | Character class | matches a, b, or c |
[^abc] | Negated class | anything except a, b, c |
(abc) | Capturing group | |
(?:abc) | Non-capturing group | |
a|b | Alternation (or) | matches a or b |
\b | Word boundary | \bword\b |
(?=...) | Positive lookahead | |
(?!...) | Negative lookahead | |
(?<=...) | Positive lookbehind |
Character Classes
[aeiou] # Any vowel
[A-Z] # Any uppercase letter
[a-z0-9] # Lowercase letter or digit
[^0-9] # Anything that is NOT a digit
[\w\-] # Word character or hyphen (useful for slugs)
[.\-+] # Literal dot, hyphen, or plus (inside [] most chars are literal)
Shorthand classes work in most engines:
\d = [0-9]
\D = [^0-9]
\w = [a-zA-Z0-9_]
\W = [^a-zA-Z0-9_]
\s = [ \t\r\n\f\v]
\S = [^ \t\r\n\f\v]
Anchors
^hello # "hello" at start of string
world$ # "world" at end of string
^hello world$ # Exact full-string match
\bhello\b # "hello" as a whole word (not "helloworld")
In multiline mode (m flag), ^ and $ match start/end of each line, not the whole string.
Quantifiers
a* # 0 or more a's (greedy)
a+ # 1 or more a's (greedy)
a? # 0 or 1 a (greedy)
a{3} # Exactly 3 a's
a{2,5} # Between 2 and 5 a's (greedy)
a{2,} # 2 or more a's
# Lazy (non-greedy) — match as few as possible
a*? # 0 or more, lazy
a+? # 1 or more, lazy
a{2,5}? # Between 2 and 5, lazy
Greedy vs lazy matters when matching HTML or nested structures:
const html = "<b>bold</b> and <b>more bold</b>";
// Greedy — matches from first <b> to last </b>
html.match(/<b>.*<\/b>/); // "<b>bold</b> and <b>more bold</b>"
// Lazy — matches each <b>...</b> pair
html.match(/<b>.*?<\/b>/g); // ["<b>bold</b>", "<b>more bold</b>"]
Groups and Capturing
// Capturing group — ()
const match = "2026-06-15".match(/(\d{4})-(\d{2})-(\d{2})/);
// match[1] = "2026", match[2] = "06", match[3] = "15"
// Named capturing group — (?<name>...)
const match2 = "2026-06-15".match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
// match2.groups = { year: "2026", month: "06", day: "15" }
// Non-capturing group — (?:...)
"color".match(/(?:colo(?:u?)r)/); // groups without capturing overhead
Lookahead and Lookbehind
These are zero-width assertions — they check context without consuming characters:
// Positive lookahead: X followed by Y
"foobar".match(/foo(?=bar)/); // matches "foo" only if followed by "bar"
// Negative lookahead: X not followed by Y
"foo123".match(/foo(?!bar)/); // matches "foo" only if NOT followed by "bar"
// Positive lookbehind: Y preceded by X
"$100".match(/(?<=\$)\d+/); // matches "100" — the digits after $
// Negative lookbehind
"100px".match(/(?<!\$)\d+/); // matches digits NOT preceded by $
Password strength check using lookaheads:
// At least 8 chars, one uppercase, one digit, one special char
const strongPassword = /^(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$/;
strongPassword.test("Secure1!"); // true
strongPassword.test("weak"); // false
Flags
/pattern/i // Case-insensitive
/pattern/g // Global — find all matches
/pattern/m // Multiline — ^ and $ match line start/end
/pattern/s // Dotall — . matches newlines too
/pattern/u // Unicode mode (recommended for modern code)
/pattern/gi // Case-insensitive + global
Common Patterns
Email Address
^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$
This is a simplified validation. The true RFC 5322 email regex is 6,300 characters long. For production, validate by sending a confirmation email.
URL
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)
US Phone Number
^(\+1[\s.-]?)?\(?[2-9]\d{2}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
Matches: (555) 123-4567, +1-555-123-4567, 5551234567
Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
IPv4 Address
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
Hex Color
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
Slug (URL path segment)
^[a-z0-9]+(?:-[a-z0-9]+)*$
Credit Card Number (basic)
^(?:4\d{12}(?:\d{3})?|5[1-5]\d{14}|3[47]\d{13}|6(?:011|5\d{2})\d{12})$
Semantic Version
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$
JavaScript Usage
// Test (boolean)
/^\d{4}$/.test("2026"); // true
// Find first match
"hello world".match(/\w+/); // ["hello"]
// Find all matches
"hello world".match(/\w+/g); // ["hello", "world"]
// Replace
"2026-06-15".replace(/(\d{4})-(\d{2})-(\d{2})/, "$3/$2/$1");
// "15/06/2026"
// Split
"a,b,,c".split(/,+/); // ["a", "b", "c"]
// Named groups
const { groups } = "2026-06-15".match(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/);
// groups.y = "2026"
Python Usage
import re
# Search (first match)
re.search(r"\d+", "abc123def") # Match object
# Find all
re.findall(r"\d+", "a1b2c3") # ["1", "2", "3"]
# Substitute
re.sub(r"\s+", " ", "too many spaces") # "too many spaces"
# Named groups
m = re.search(r"(?P<year>\d{4})-(?P<month>\d{2})", "2026-06")
m.group("year") # "2026"
# Compile for reuse
pattern = re.compile(r"^\d{4}-\d{2}-\d{2}quot;)
pattern.match("2026-06-15")
Try Patterns Now
Test and debug your regular expressions with the Regex Tester Tool — real-time highlighting, match groups, and explanation of each pattern component.
More Tools from Cosyslabs
- Routine Toolkit — Everyday utilities including a word counter (useful after regex-based text extraction), loan calculator, and date calculator.
- Unit Convert All — When regex extracts numeric values from text, convert between units before displaying them to users.
- Rough Estimator — Estimate development timelines for projects involving complex text parsing or ETL pipelines where regex-heavy processing is a key concern.
- Cosyslabs — The studio behind Dev Tools !, PDF Convert All, Astrilio, CastFleet, and more.