All cheat sheets

Regular Expressions Cheat Sheet

Regex reference: anchors, character classes, quantifiers, groups, lookaround, and common patterns.

Languages
regex
regexp

Regular expressions describe text patterns using anchors, character classes, repetition, and grouping. Dialects differ, so verify engine support—especially for lookbehind, named groups, and Unicode behavior—before shipping a pattern.

Anchors and boundaries

Anchors match positions rather than consuming characters. With multiline mode, ^ and $ can apply to individual lines instead of only the complete input.

Table
TokenMeaning
^Start of input, or line with multiline mode.
$End of input, or line with multiline mode.
\bWord boundary.
\BNot a word boundary.
\AAbsolute start in engines that support it.
\zAbsolute end in engines that support it.
regex
^INFO:.*$
\bcat\b

Character classes

A class matches one character from a set. Shorthand classes are convenient but often ASCII- or engine-dependent; use explicit Unicode properties when the engine provides them.

Table
PatternMatches
[abc]a, b, or c.
[^abc]Any character except a, b, or c.
[a-z]One lowercase ASCII letter.
\dA digit; exact Unicode scope depends on the engine.
\wA word character; exact scope depends on the engine.
\sWhitespace.
.Any character except a newline in common modes.
regex
^[A-Z][a-z]+$
[^,]+,
\d{4}-\d{2}-\d{2}

Quantifiers

Quantifiers control how many times the preceding token may repeat. Most are greedy: they consume as much as possible while still allowing the rest of the pattern to match. Add ? for a lazy form.

Table
QuantifierMeaning
*Zero or more.
+One or more.
?Zero or one.
{n}Exactly n.
{n,}At least n.
{n,m}Between n and m.
*?, +?, {n,m}?Lazy variants in common backtracking engines.
regex
<[^>]+>
".*?"
[A-F0-9]{2}(?::[A-F0-9]{2}){5}

Groups and alternation

Capturing groups retain matched text for replacement or code. Use non-capturing groups when you need grouping but not a capture, and name groups when the engine supports named syntax.

regex
^(?<scheme>https?)://(?<host>[^/]+)(?<path>/.*)?$
Table
SyntaxPurpose
(abc)Capturing group.
(?:abc)Non-capturing group.
(?<name>abc)Named capture in several modern engines.
`abcdef`
\1Backreference to capture 1 in many engines.

Lookaround

Lookaround asserts nearby text without consuming it. Positive forms require a condition; negative forms reject it. Lookbehind availability and length rules vary by engine.

regex
\d+(?= USD)
(?<!un)happy
^(?!.*password).+$
(?<=#)[A-Za-z0-9_-]+
Table
SyntaxMeaning
(?=...)Positive lookahead.
(?!...)Negative lookahead.
(?<=...)Positive lookbehind.
(?<!...)Negative lookbehind.

Common patterns

These patterns are practical starting points, not complete validators for every international or protocol edge case. Apply length limits and semantic validation in code too.

Table
UsePattern
Email-like address^[^\s@]+@[^\s@]+\.[^\s@]+$
HTTP(S) URL^https?://[^\s]+$
IPv4 candidate^(?:\d{1,3}\.){3}\d{1,3}$
US-style phone`^+?1?[ -]?(?:(\d{3})
ISO date^\d{4}-\d{2}-\d{2}$
Hex color`^#(?:[0-9A-Fa-f]{3}

Flags and testing

Flags change matching behavior and are usually specified outside the pattern. Common names are shown in JavaScript-style notation; command-line and library syntax differs.

Table
FlagEffect
gFind all matches rather than stopping after the first.
iIgnore case.
mMake ^ and $ line-aware.
sLet . match newlines.
uEnable Unicode-aware behavior in engines that support it.
ySticky matching from the current position in JavaScript.
javascript
const pattern = /^(?<key>[A-Za-z_][A-Za-z0-9_]*)\s*=\s*(?<value>.*)$/m;
const match = pattern.exec("mode = safe");
console.log(match?.groups);

References