Reference

Regex Cheat Sheet — Regular Expression Quick Reference

A complete regular expression cheat sheet covering anchors, character classes, quantifiers, groups, lookaheads, and common patterns. Every example links directly to the RegEx tester.

Anchors

SyntaxDescriptionTry it
^Start of string (or line with m flag)^Hello
$End of string (or line with m flag)world$
\bWord boundary\bcat\b
\BNon-word boundary\Bcat\B

Character Classes

SyntaxDescriptionTry it
.Any character except newline
\dDigit [0-9]\d+
\DNon-digit [^0-9]
\wWord character [a-zA-Z0-9_]\w+
\WNon-word character
\sWhitespace (space, tab, newline)\s+
\SNon-whitespace
[abc]Any of a, b, or c[aeiou]
[^abc]Anything except a, b, or c[^aeiou]
[a-z]Character in range a to z[a-zA-Z]

Quantifiers

SyntaxDescriptionTry it
*0 or more (greedy)ab*c
+1 or more (greedy)\d+
?0 or 1 — makes preceding optionalcolou?r
{n}Exactly n repetitions\d{4}
{n,}n or more repetitions\w{3,}
{n,m}Between n and m repetitions\d{2,4}
*?0 or more (lazy — shortest match)<.*?>
+?1 or more (lazy)\d+?

Groups & References

SyntaxDescriptionTry it
(abc)Capturing group(\d+)-(\d+)
(?:abc)Non-capturing group(?:foo|bar)
\1Backreference to group 1(\w+)\s\1
|Alternation (OR)cat|dog

Lookaheads & Lookbehinds

SyntaxDescriptionTry it
(?=...)Positive lookahead — followed by\d(?= dollars)
(?!...)Negative lookahead — not followed by\d(?! dollars)
(?<=...)Positive lookbehind — preceded by(?<=\$)\d+
(?<!...)Negative lookbehind — not preceded by(?<!\$)\d+

Flags

SyntaxDescriptionTry it
gGlobal — find all matches, not just the first
iCase-insensitive match
mMultiline — ^ and $ match start/end of each line
sDotall — . matches newline characters too

Common Patterns

PatternRegexTry it
Email address[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}Try it →
URL (http/https)https?:\/\/[\w\-.]+(:\d+)?(\/[\w\-._~:/?#[\]@!$&'()*+,;=%]*)?Try it →
IPv4 address\b(\d{1,3}\.){3}\d{1,3}\bTry it →
Date (YYYY-MM-DD)\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])Try it →
Time (HH:MM)(?:[01]\d|2[0-3]):[0-5]\dTry it →
Hex colour#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})\bTry it →
UK postcode[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}Try it →
US ZIP code\b\d{5}(?:-\d{4})?\bTry it →
Phone (intl, loose)\+?[\d\s\-().]{7,20}Try it →
Digits only^\d+$Try it →
Alphanumeric only^[a-zA-Z0-9]+$Try it →
Slug (URL-safe)^[a-z0-9]+(?:-[a-z0-9]+)*$Try it →
HTML tag<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>.*?<\/\1>Try it →
Repeated word\b(\w+)\s+\1\bTry it →
Empty lines^\s*$Try it →

Test any regex pattern live in your browser

Paste a pattern, enter test text, and see matches highlighted instantly

Open RegEx Tester →