Regex Cheatsheet

Interactive reference with live tester β€” click any entry to insert the pattern and see matches highlighted in real time.

⚑ Live Tester
No pattern Embed this β†’
Pattern
Click flags to toggle
Test String
Result (matches highlighted)
Powered by Ship This Weekend β€” free regex tester Β· Embed on your site
Anchors & Boundaries
^
Start of string (or line with m)
Without m flag, matches start of entire string
^Hello matches "Hello world" but not "say Hello"
↑ insert
$
End of string (or line with m)
Without m flag, matches end of entire string
world$ matches "hello world" but not "worldwide"
↑ insert
\b
Word boundary
Position between \w and \W (or string edge)
\bcat\b matches "cat" not "catch" or "concatenate"
↑ insert
\B
Non-word boundary
Position NOT between \w and \W
\Bcat matches "scat" or "concatenate" not "cat"
↑ insert
Character Classes
.
Any character (except newline)
With s flag, also matches \n
a.c matches "abc", "a1c", "a!c"
↑ insert
\d
Any digit β€” [0-9]
Equivalent to [0-9]
\d+ matches "123", "42", "007"
↑ insert
\D
Any non-digit β€” [^0-9]
Matches letters, spaces, punctuation
↑ insert
\w
Word character β€” [a-zA-Z0-9_]
Letters, digits, underscore
\w+ matches "hello", "world_1", "abc123"
↑ insert
\W
Non-word character β€” [^a-zA-Z0-9_]
Spaces, punctuation, etc.
↑ insert
\s
Whitespace β€” space, tab, newline
Equivalent to [ \t\n\r\f\v]
↑ insert
\S
Non-whitespace
Any character that isn't whitespace
↑ insert
[aeiou]
Character set β€” any listed character
Match a, e, i, o, or u
[aeiou]+ matches "e", "ai", "ou"
↑ insert
[a-z]
Character range β€” a through z
Ranges: [a-z], [A-Z], [0-9], [a-zA-Z]
↑ insert
[^abc]
Negated set β€” any except listed
^ inside [] negates the class
[^aeiou] matches consonants and non-letters
↑ insert
Quantifiers
*
Zero or more (greedy)
Matches as many as possible
a* matches "", "a", "aaa"
↑ insert
+
One or more (greedy)
At least one required
a+ matches "a", "aaa" but not ""
↑ insert
?
Zero or one (optional)
Makes the preceding token optional
colou?r matches "colour" and "color"
↑ insert
{n}
Exactly n times
Replace n with a number
\d{4} matches "1234" but not "123"
↑ insert
{n,m}
Between n and m times (inclusive)
{2,} means "2 or more"
\d{2,4} matches "12", "123", "1234"
↑ insert
*? +? ??
Lazy (non-greedy) quantifiers
Match as few as possible
<.+?> matches "<div>" not "<div>hello</div>"
↑ insert
Groups & References
(abc)
Capturing group
Captures matched text as \1, \2, etc.
(foo|bar) matches "foo" or "bar", captured
↑ insert
(?:abc)
Non-capturing group
Groups for quantifiers/alternation, no capture
(?:foo|bar)+ matches "foofoo", "foobar", "bar"
↑ insert
(?<name>abc)
Named capturing group
Access via match.groups.name in JS
(?<year>\d{4}) captures year by name
↑ insert
\1 \2
Backreference
Refers to what group 1, 2, etc. matched
(ha)\1 matches "haha" (ha + same ha)
↑ insert
a|b
Alternation (OR)
Matches a or b β€” left to right
cat|dog matches "cat" or "dog"
↑ insert
Lookahead & Lookbehind
(?=abc)
Positive lookahead
Asserts abc follows β€” not consumed
\d+(?= dollars) matches amounts before " dollars"
↑ insert
(?!abc)
Negative lookahead
Asserts abc does NOT follow
\d+(?! dollars) matches amounts NOT before " dollars"
↑ insert
(?<=abc)
Positive lookbehind
Asserts abc precedes β€” not consumed
(?<=\$)\d+ matches digits after "$"
↑ insert
(?<!abc)
Negative lookbehind
Asserts abc does NOT precede
(?<!\$)\d+ matches digits not preceded by "$"
↑ insert
Common Patterns β€” Click to Test
Email Address
^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$
Validates most real-world email addresses. Not RFC 5322 complete but handles 99% of cases.
βœ“ hello@example.com βœ“ user.name+tag@domain.co.uk
URL (http/https)
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&\/=]*)
Matches http and https URLs with optional www, path, query string.
βœ“ https://example.com/path?q=test
US Phone Number
\+?1?\s*\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}
Matches common US phone formats: dashes, dots, spaces, parentheses, optional country code.
βœ“ 415-555-0182 βœ“ (650) 555-9041 βœ“ +1 800 555 1234
Date (YYYY-MM-DD or YYYY/MM/DD)
\d{4}[-\/]\d{2}[-\/]\d{2}
ISO 8601 date format. Accepts dash or slash separator.
βœ“ 2024-05-30 βœ“ 2024/01/01
IPv4 Address
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$
Validates IPv4 addresses with proper range checking (0–255 per octet).
βœ“ 192.168.1.1 βœ— 256.0.0.1
Hex Color
#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})\b
CSS hex color codes β€” 3 or 6 digit shorthand.
βœ“ #ff6b6b βœ“ #abc βœ“ #FFFFFF
URL Slug
^[a-z0-9]+(?:-[a-z0-9]+)*$
Valid URL slugs β€” lowercase letters, digits, hyphens. No leading/trailing hyphens.
βœ“ hello-world βœ“ slug-123 βœ— Invalid Slug
Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Requires: 1 lowercase, 1 uppercase, 1 digit, 1 special character, 8+ chars minimum.
βœ“ Password1! βœ— weakpass βœ— NoSpecial1
Escape Sequences
\.
Literal dot
Backslash escapes special regex chars
\. matches "." only, not any character
↑ insert
\n
Newline
Also: \r (carriage return), \t (tab)
↑ insert
\t
Tab character
Useful for parsing TSV files
↑ insert
\\
Literal backslash
In JS strings: '\\\\' = regex \\
\\ matches "\" in Windows paths
↑ insert

Regex Tips