Click any item to expand the explanation and examples. Try patterns live with our Regex Tester.
π€ Character Classes
. (dot) β Any character basics
h.t β matches "hat", "hot", "hit", "h9t" .. β matches any two charactersUse
\. to match a literal dot.
\d \w \s β Shorthand classes basics
\d β digit [0-9] \D β non-digit [^0-9] \w β word char [a-zA-Z0-9_] \W β non-word char \s β whitespace [ \t\n\r\f] \S β non-whitespaceExample:
\d{3}-\d{4} matches "555-1234".
[abc] β Character sets basics
[aeiou] β any vowel [a-z] β any lowercase letter [A-Z] β any uppercase letter [0-9] β any digit [a-zA-Z] β any letter [^aeiou] β any NON-vowel (^ negates)Example:
gr[ae]y matches "gray" and "grey".
π’ Quantifiers
* + ? β Basic quantifiers quantifiers
* β 0 or more (greedy) + β 1 or more (greedy) ? β 0 or 1 (optional)
colou?r β "color" or "colour" go+gle β "gogle", "google", "gooogle"... ab*c β "ac", "abc", "abbc"...
{n} {n,m} β Exact quantifiers quantifiers
{3} β exactly 3
{2,5} β between 2 and 5
{3,} β 3 or more
\d{3} β "123", "999"
\w{2,4} β 2 to 4 word characters
[a-z]{5,} β 5+ lowercase letters
*? +? ?? β Lazy quantifiers quantifiers
? after a quantifier to make it lazy (match as few as possible).
# Greedy (default): matches as MUCH as possible ".*" on <a>foo</a><b>bar</b> β matches "<a>foo</a><b>bar</b>"Lazy: matches as LITTLE as possible
β.*?β on <a>foo</a><b>bar</b> β matches β<a>β then β</a>β etc.
β Anchors
^ $ β Start and end anchors
^hello β line starts with "hello" world$ β line ends with "world" ^exact$ β entire string is "exact"With the
m (multiline) flag, ^ and $ match start/end of each line.
\b β Word boundary anchors
\bcat\b β matches "cat" but NOT "category" or "scat" \bpre β matches words starting with "pre" ing\b β matches words ending with "ing"
π₯ Groups & Alternation
(abc) β Capturing groups groups
(foo) β captures "foo" as group 1
(foo)(bar) β group 1: "foo", group 2: "bar"
(\d{3})-(\d{4}) β captures area code and number separately
Reference captured groups with \1, \2 in the pattern or $1, $2 in replacements.
(?:abc) β Non-capturing groups groups
(?:foo|bar)baz β matches "foobaz" or "barbaz"
but doesn't capture "foo"/"bar"
Use when you need grouping for alternation or quantifiers but don't need the captured value.
| β Alternation (OR) groups
cat|dog β "cat" or "dog" (red|blue) car β "red car" or "blue car" ^(GET|POST|PUT) β line starts with HTTP method
(?<name>) β Named groups groups
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
In JavaScript:
const m = β2026-03-14β.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
m.groups.year // β2026β
m.groups.month // β03β
\1 \2 β Backreferences groups
(\w+)\s+\1 β matches repeated words: "the the", "is is" (["']).*?\1 β matches quoted strings with matching quotes
π Lookahead & Lookbehind
(?=...) β Positive lookahead lookaround
\d+(?=px) β matches "12" in "12px" but not "12em" foo(?=bar) β matches "foo" only if followed by "bar"
(?!...) β Negative lookahead lookaround
\d+(?!px) β matches "12" in "12em" but not "12px" foo(?!bar) β matches "foo" NOT followed by "bar"
(?<=...) β Positive lookbehind lookaround
(?<=\$)\d+ β matches "50" in "$50" but not "50" (?<=@)\w+ β matches username after @ in emails
(?<!...) β Negative lookbehind lookaround
(?<!\$)\d+ β matches "50" when NOT preceded by $ (?<!un)happy β matches "happy" but not "unhappy"
π© Flags
g i m s u β Regex flags flags
g β global: find all matches, not just the first i β case-insensitive: /hello/i matches "Hello", "HELLO" m β multiline: ^ and $ match line starts/ends s β dotAll: . also matches newline characters u β unicode: enable full Unicode support
# JavaScript
/hello/gi β global + case-insensitive
new RegExp("hello", "gi")
Python
re.findall(rβhelloβ, text, re.IGNORECASE)
π― Common Patterns
Email address patterns
[\w.+-]+@[\w-]+\.[\w.]+Matches:
user@example.com, first.last+tag@sub.domain.org
URL patterns
https?://[\w\-.]+(:\d+)?(/[\w\-./?%&=]*)?Matches:
https://example.com, http://localhost:3000/path?q=1
Phone number patterns
# US formats
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Matches: (555) 123-4567, 555-123-4567, 555.123.4567, 5551234567
IP address (IPv4) patterns
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Matches: 192.168.1.1, 10.0.0.255
Note: This doesn't validate ranges (0-255). For strict validation, use a more complex pattern or validate in code.
Date (YYYY-MM-DD) patterns
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Matches: 2026-03-14, 2025-12-31
Quick Reference Table
| Pattern | Meaning | Example |
|---|---|---|
. | Any character | h.t β hat, hot |
\d \w \s | Digit, word, space | \d+ β 123 |
[abc] | Character set | [aeiou] β vowels |
[^abc] | Negated set | [^0-9] β non-digit |
* + ? | 0+, 1+, 0 or 1 | go+d β god, good |
{n,m} | n to m times | \d{2,4} β 12, 1234 |
^ $ | Start, end | ^hello$ |
\b | Word boundary | \bcat\b |
(abc) | Capture group | (\d+)px |
(?:abc) | Non-capturing | `(?:a |
a|b | Alternation | cat|dog |
(?=...) | Lookahead | \d+(?=px) |
(?<=...) | Lookbehind | (?<=\$)\d+ |
Try these patterns live β Regex Tester