Regex Tester

Regex Cheat Sheet: Quick Reference for Regular Expressions

Interactive regex cheat sheet with syntax tables for metacharacters, quantifiers, anchors, groups, lookaheads, and flags. Click any example to test it live.

100% client-side. Your data never leaves your browser.

//g
2 matches
Contact us at support@example.com or sales@company.co.uk for help. Invalid emails: @missing.com, no-at-sign, user@.bad
Match Details

Related Tools

Characters

PatternMatchesExample match
.Any character except newlinea.c matches abc, a1c, a-c
\dAny digit [0-9]\d{3} matches 123
\DAny nondigit\D+ matches abc in abc123
\wWord character [a-zA-Z0-9_]\w+ matches hello_world
\WNonword character\W matches @ in user@host
\sWhitespace (space, tab, newline)\s+ matches spaces between words
\SNonwhitespace\S+ matches hello
\tTab character
\nNewline

Quantifiers

PatternMeaningExample
*0 or more (greedy)ab*c matches ac, abc, abbc
+1 or more (greedy)ab+c matches abc, abbc but not ac
?0 or 1 (optional)colou?r matches color and colour
{3}Exactly 3\d{3} matches 123 but not 12
{2,5}Between 2 and 5\w{2,5} matches ab through abcde
{3,}3 or more\d{3,} matches 123, 1234, 12345
*?0 or more (lazy)".*?" matches shortest quoted string
+?1 or more (lazy)<.+?> matches a single HTML tag

Anchors

PatternMatchesExample
^Start of string (or line with m flag)^Hello matches Hello world
$End of string (or line with m flag)world$ matches Hello world
\bWord boundary\bcat\b matches cat but not cats or concatenate
\BNonword boundary\Bcat\B matches concatenate but not cat

Groups and Alternation

PatternMeaningExample
(abc)Capturing group(ha)+ matches haha, captures ha
(?:abc)Noncapturing group(?:ha)+ matches haha, no capture
(?<name>abc)Named capture group(?<year>\d{4}) captures year by name
a|bAlternation (or)cat|dog matches cat or dog
\1Back reference to group 1(\w+)\s\1 matches the the

Character Classes

PatternMatchesExample
[abc]Any of a, b, or c[aeiou] matches any vowel
[^abc]Not a, b, or c[^0-9] matches any nondigit
[a-z]Range: a through z[a-zA-Z] matches any letter
[a-zA-Z0-9]AlphanumericSame as \w minus underscore

Lookaheads and Lookbehinds

Zero width assertions: they check a condition without consuming characters.

PatternTypeMeaningExample
(?=foo)Positive lookaheadFollowed by foo\d+(?= USD) matches 100 in 100 USD
(?!foo)Negative lookaheadNOT followed by foo\d+(?! USD) matches 200 in 200 EUR
(?<=foo)Positive lookbehindPreceded by foo(?<=\$)\d+ matches 50 in $50
(?<!foo)Negative lookbehindNOT preceded by foo(?<!\$)\d+ matches 50 in EUR 50

Lookbehinds must be fixed length in most engines. JavaScript added variable length lookbehinds in ES2018, but Safari support lagged until 2023.

Flags

FlagNameEffect
gGlobalFind all matches, not just the first
iCase insensitivea matches A and a
mMultiline^ and $ match line start/end, not just string start/end
sDotall. matches newline characters too
uUnicodeEnable full Unicode matching and proper surrogate pair handling

Common Patterns

These cover the majority of real world validation needs. All patterns are simplified for readability. Production systems should use dedicated parsers for complex formats like email (RFC 5322).

Email (simplified)

[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}

URL

https?://[^\s/$.?#].[^\s]*

IPv4 address

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

Does not validate that octets are 0-255. For strict validation, use: \b(25[0-5]|2[0-4]\d|[01]?\d\d?)(\.(25[0-5]|2[0-4]\d|[01]?\d\d?)){3}\b

Date (YYYY-MM-DD)

\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])

Hex color

#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})\b

Phone number (US, flexible)

\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}

Matches (555) 123-4567, 555-123-4567, 555.123.4567, and 5551234567.

Greedy vs Lazy

The most common regex surprise. Given the input <b>bold</b> and <i>italic</i>:

PatternMatchWhy
<.+><b>bold</b> and <i>italic</i>Greedy: matches from first < to last >
<.+?><b>, </b>, <i>, </i>Lazy: matches from each < to the nearest >

Default behavior is greedy. Append ? to any quantifier to make it lazy.

JavaScript-Specific Notes