Regular Expressions Cheat Sheet
Regex reference: anchors, character classes, quantifiers, groups, lookaround, and common patterns.
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.
| Token | Meaning |
|---|---|
^ | Start of input, or line with multiline mode. |
$ | End of input, or line with multiline mode. |
\b | Word boundary. |
\B | Not a word boundary. |
\A | Absolute start in engines that support it. |
\z | Absolute end in engines that support it. |
^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.
| Pattern | Matches |
|---|---|
[abc] | a, b, or c. |
[^abc] | Any character except a, b, or c. |
[a-z] | One lowercase ASCII letter. |
\d | A digit; exact Unicode scope depends on the engine. |
\w | A word character; exact scope depends on the engine. |
\s | Whitespace. |
. | Any character except a newline in common modes. |
^[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.
| Quantifier | Meaning |
|---|---|
* | 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. |
<[^>]+>
".*?"
[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.
^(?<scheme>https?)://(?<host>[^/]+)(?<path>/.*)?$
| Syntax | Purpose |
|---|---|
(abc) | Capturing group. |
(?:abc) | Non-capturing group. |
(?<name>abc) | Named capture in several modern engines. |
| `abc | def` |
\1 | Backreference 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.
\d+(?= USD)
(?<!un)happy
^(?!.*password).+$
(?<=#)[A-Za-z0-9_-]+
| Syntax | Meaning |
|---|---|
(?=...) | 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.
| Use | Pattern |
|---|---|
| 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.
| Flag | Effect |
|---|---|
g | Find all matches rather than stopping after the first. |
i | Ignore case. |
m | Make ^ and $ line-aware. |
s | Let . match newlines. |
u | Enable Unicode-aware behavior in engines that support it. |
y | Sticky matching from the current position in 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);