Skip to content
browserutils
Cheatsheet

Regex Cheatsheet — Regular Expression Syntax Reference

Complete regular expression reference with metacharacters, quantifiers, anchors, groups, lookaheads, flags, and practical examples for pattern matching.

Reference

Regular expressions (regex) are patterns used to match character combinations in strings. They appear in virtually every programming language and many command-line tools. This reference covers the full regex syntax you need for day-to-day development.

Metacharacters

These special characters have meaning beyond their literal value.

CharacterMeaningExampleMatches
.Any character except newlinea.cabc, a1c, a-c
\dAny digit (0-9)\d{3}123, 456
\DAny non-digit\D+abc, ---
\wWord character (a-z, A-Z, 0-9, _)\w+hello_42
\WNon-word character\W@, #,
\sWhitespace (space, tab, newline)\s+ , \t\n
\SNon-whitespace\S+hello
\bWord boundary\bcat\bcat in “the cat sat”
\BNon-word boundary\Bcat\Bcat in “concatenate”
\\Escape special character\.literal .

Quantifiers

Quantifiers control how many times a pattern element repeats.

QuantifierMeaningExampleMatches
*0 or moreab*cac, abc, abbc
+1 or moreab+cabc, abbc (not ac)
?0 or 1colou?rcolor, colour
{n}Exactly n\d{4}2026
{n,}n or more\d{2,}42, 123, 9999
{n,m}Between n and m\d{2,4}42, 123, 9999

Lazy (non-greedy) versions: Add ? after any quantifier to match as few characters as possible.

GreedyLazyBehavior
.*.*?Match as little as possible
.+.+?Match 1+, preferring fewer
.{2,5}.{2,5}?Match 2-5, preferring 2

Anchors

Anchors match positions, not characters.

AnchorMeaningExampleMatches
^Start of string (or line with m flag)^HelloHello world
$End of string (or line with m flag)world$Hello world
\bWord boundary\bword\bwhole word only
\AStart of string (never line)\AStartonly at absolute start
\ZEnd of string (never line)end\Zonly at absolute end

Character Classes

SyntaxMeaningExampleMatches
[abc]Any of a, b, or c[aeiou]any vowel
[^abc]Not a, b, or c[^0-9]any non-digit
[a-z]Range: a through z[A-Za-z]any letter
[a-zA-Z0-9]Alphanumeric[a-zA-Z0-9_]same as \w
[\s\S]Any character including newline[\s\S]*everything

Groups and Capturing

SyntaxMeaningExample
(abc)Capturing group(foo)bar captures foo
(?:abc)Non-capturing group(?:foo)bar groups without capture
(?<name>abc)Named capturing group(?<year>\d{4})
\1Backreference to group 1(a)\1 matches aa
\k<name>Named backreference\k<year>
(a|b)Alternation (OR)(cat|dog) matches either

Lookahead and Lookbehind

These match a position based on what comes before or after, without consuming characters.

SyntaxNameExampleMatches
(?=abc)Positive lookahead\d(?=px)5 in 5px
(?!abc)Negative lookahead\d(?!px)5 in 5em
(?<=abc)Positive lookbehind(?<=\$)\d+100 in $100
(?<!abc)Negative lookbehind(?<!\$)\d+100 in €100

Flags / Modifiers

Flags change how the regex engine processes the pattern.

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitiveA matches a
mMultiline^ and $ match line starts/ends
sDotall / Single-line. matches newline characters
uUnicodeEnable full Unicode matching
xExtendedIgnore whitespace, allow comments
yStickyMatch only at lastIndex position

Common Patterns

These battle-tested patterns cover frequent validation tasks.

# Email (simplified)
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

# URL
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)

# IPv4 Address
\b(?:\d{1,3}\.){3}\d{1,3}\b

# Date (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])

# Hex color
#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})

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

# Strong password (8+ chars, upper, lower, digit, special)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$

# HTML tag
<\/?[\w\s]*>|<.+[\W]>

# Whitespace trimming
^\s+|\s+$

POSIX Character Classes

Used in tools like grep, sed, and awk.

ClassEquivalentMeaning
[:alpha:][a-zA-Z]Letters
[:digit:][0-9]Digits
[:alnum:][a-zA-Z0-9]Alphanumeric
[:space:][\s]Whitespace
[:upper:][A-Z]Uppercase letters
[:lower:][a-z]Lowercase letters
[:punct:]Punctuation characters
[:print:]Printable characters

Test your patterns in real time with the Regex Tester or generate patterns automatically with the Regex Generator.