📖 Guide
Regex Cheat Sheet — Complete Regular Expression Reference
Every regex pattern, syntax, and technique you need. From basics to advanced lookaheads, with real examples.
88 commands across 10 categories
Character ClassesAnchorsQuantifiersGroups & CapturingLookahead & LookbehindAlternation & LogicFlags / ModifiersCommon PatternsJavaScript String MethodsAdvanced Techniques
Character Classes
| Command | Description |
|---|---|
. | Match any character except newline |
\de.g. \d{3} → 123 | Match any digit [0-9] |
\D | Match any non-digit |
\w | Match word character [a-zA-Z0-9_] |
\W | Match non-word character |
\s | Match whitespace (space, tab, newline) |
\S | Match non-whitespace |
[abc]e.g. [aeiou] → vowels | Match any character in the set |
[^abc] | Match any character NOT in the set |
[a-z] | Match character range (lowercase letters) |
[a-zA-Z0-9] | Match alphanumeric characters |
[\d\s] | Combine classes — digit or whitespace |
\t | Match tab character |
\n | Match newline character |
\\ | Match literal backslash |
\. | Match literal dot (escaped metacharacter) |
Anchors
| Command | Description |
|---|---|
^e.g. ^Hello → matches 'Hello world' | Start of string (or line with m flag) |
$e.g. world$ → matches 'Hello world' | End of string (or line with m flag) |
\be.g. \bcat\b → 'cat' but not 'category' | Word boundary |
\B | Not a word boundary |
\A | Start of string (not affected by multiline flag) |
\Z | End of string (not affected by multiline flag) |
Quantifiers
| Command | Description |
|---|---|
*e.g. a* → '', 'a', 'aaa' | Match 0 or more (greedy) |
+e.g. a+ → 'a', 'aaa' | Match 1 or more (greedy) |
?e.g. colou?r → 'color', 'colour' | Match 0 or 1 (optional) |
{n}e.g. \d{4} → '2024' | Match exactly n times |
{n,}e.g. \d{2,} → '42', '123' | Match n or more times |
{n,m}e.g. \d{2,4} → '42', '123', '1234' | Match between n and m times |
*? | Match 0 or more (lazy/non-greedy) |
+? | Match 1 or more (lazy/non-greedy) |
?? | Match 0 or 1 (lazy) |
{n,m}? | Match between n and m (lazy) |
Groups & Capturing
| Command | Description |
|---|---|
(pattern)e.g. (\d{3})-(\d{4}) | Capturing group — captures the match |
(?:pattern) | Non-capturing group — groups without capturing |
(?<name>pattern)e.g. (?<year>\d{4})-(?<month>\d{2}) | Named capturing group |
\1, \2e.g. (\w+)\s\1 → 'the the' | Backreference to captured group |
\k<name> | Backreference by name |
$1, $2 | Reference captured group in replacement string |
$& | Reference entire match in replacement |
$` | Reference text before match in replacement |
$' | Reference text after match in replacement |
Lookahead & Lookbehind
| Command | Description |
|---|---|
(?=pattern)e.g. \d(?=px) → '5' in '5px' | Positive lookahead — match if followed by pattern |
(?!pattern)e.g. \d(?!px) → '3' in '3em' | Negative lookahead — match if NOT followed by pattern |
(?<=pattern)e.g. (?<=\$)\d+ → '100' in '$100' | Positive lookbehind — match if preceded by pattern |
(?<!pattern) | Negative lookbehind — match if NOT preceded by pattern |
(?<=\$)\d+(?=\.\d{2})e.g. '$19.99' → '19' | Combined lookaround — dollar amount |
Alternation & Logic
| Command | Description |
|---|---|
a|be.g. cat|dog → 'cat' or 'dog' | Match a OR b |
(a|b)ce.g. (re|un)do → 'redo' or 'undo' | Group alternation with continuation |
(?:jpg|png|gif)e.g. \.(jpg|png|gif)$ | Non-capturing alternation |
Flags / Modifiers
| Command | Description |
|---|---|
g | Global — find all matches, not just the first |
i | Case-insensitive matching |
m | Multiline — ^ and $ match line starts/ends |
s | Dotall — . matches newline characters too |
u | Unicode — enable full Unicode matching |
y | Sticky — match only at lastIndex position (JS) |
d | Has indices — include match index info (JS) |
(?i) | Inline flag — enable case-insensitive in pattern |
Common Patterns
| Command | Description |
|---|---|
^[\w.-]+@[\w.-]+\.\w{2,}$ | Email address (basic) |
https?://[\w.-]+(?:\.[\w]{2,})(?:/\S*)? | URL (HTTP/HTTPS) |
^\d{1,3}(\.\d{1,3}){3}$ | IPv4 address |
^\+?\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{3,4}[-.\s]?\d{3,4}$ | Phone number (international) |
^\d{4}-\d{2}-\d{2}$e.g. 2024-01-15 | Date (YYYY-MM-DD) |
^\d{2}/\d{2}/\d{4}$ | Date (DD/MM/YYYY) |
^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$e.g. #ff0000, #f00 | Hex color code |
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$ | Strong password (8+ chars, upper, lower, digit) |
^[a-z0-9-]+$ | URL slug (lowercase, numbers, hyphens) |
<[^>]+> | HTML tag (basic) |
\b\d{1,3}(,\d{3})*(\.\d+)?\be.g. 1,234,567.89 | Number with commas |
^\s+|\s+$ | Leading/trailing whitespace (for trimming) |
\s{2,} | Multiple consecutive whitespace |
JavaScript String Methods
| Command | Description |
|---|---|
str.match(/pattern/g)e.g. 'abc 123'.match(/\d+/g) → ['123'] | Find all matches, return array |
str.matchAll(/pattern/g) | Iterator of all matches with groups |
str.search(/pattern/) | Return index of first match (-1 if none) |
str.replace(/pattern/, 'new') | Replace first match |
str.replace(/pattern/g, 'new') | Replace all matches |
str.replaceAll('old', 'new') | Replace all (string or regex with g flag) |
/pattern/.test(str)e.g. /^\d+$/.test('123') → true | Return true/false if pattern matches |
/pattern/.exec(str) | Return match object with groups and index |
str.split(/pattern/)e.g. 'a, b, c'.split(/,\s*/) | Split string by regex |
new RegExp('pattern', 'flags') | Create regex from string (for dynamic patterns) |
Advanced Techniques
| Command | Description |
|---|---|
(?:(?!pattern).)* | Tempered greedy token — match anything not containing pattern |
\p{L}e.g. /\p{L}+/gu | Unicode letter (any language, requires u flag) |
\p{N} | Unicode number (any script) |
\p{Emoji} | Match emoji characters (requires u flag) |
(?<=(\d+))\s(?=\w) | Complex lookaround combination |
^(?!.*forbidden).*$ | Match line NOT containing word |
(.)\1+e.g. 'aaa' → 'aaa' | Match repeated characters |
(?=.*a)(?=.*b)(?=.*c) | Match string containing all of a, b, and c (any order) |
📖 Free, searchable command reference. Bookmark this page for quick access.