📖 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 Classes

CommandDescription
.
Match any character except newline
\d
e.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

CommandDescription
^
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)
\b
e.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

CommandDescription
*
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

CommandDescription
(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, \2
e.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

CommandDescription
(?=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

CommandDescription
a|b
e.g. cat|dog → 'cat' or 'dog'
Match a OR b
(a|b)c
e.g. (re|un)do → 'redo' or 'undo'
Group alternation with continuation
(?:jpg|png|gif)
e.g. \.(jpg|png|gif)$
Non-capturing alternation

Flags / Modifiers

CommandDescription
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

CommandDescription
^[\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+)?\b
e.g. 1,234,567.89
Number with commas
^\s+|\s+$
Leading/trailing whitespace (for trimming)
\s{2,}
Multiple consecutive whitespace

JavaScript String Methods

CommandDescription
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

CommandDescription
(?:(?!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)

More Guides

🌿
Git Commands
Complete Git command reference — from basics to advanced workflows. Searchable, with examples.
📝
Vim Commands
Complete Vim/Vi command reference — modes, motions, editing, search, and advanced features.
🐳
Docker Commands
Complete Docker & Docker Compose command reference — containers, images, volumes, networks, and orchestration.
🐧
Linux Commands
Complete Linux/Bash command reference — file management, text processing, networking, system admin, and shell scripting.
☸️
Kubernetes Commands
Complete Kubernetes & kubectl command reference — pods, deployments, services, configmaps, and cluster management.
🐍
Python Reference
Complete Python reference — syntax, data structures, string methods, file I/O, comprehensions, and common patterns.
🗃️
SQL Reference
Complete SQL reference — queries, joins, aggregation, subqueries, indexes, and database management.
🌐
Nginx Reference
Complete Nginx configuration reference — server blocks, locations, proxying, SSL, load balancing, and caching.
🔐
SSH Commands
Complete SSH reference — connections, key management, tunneling, config, SCP/SFTP, and security hardening.
👷
Jenkins Reference
Complete Jenkins reference — pipeline syntax, Jenkinsfile, plugins, CLI, agents, and CI/CD patterns.
☁️
AWS CLI Reference
Complete AWS CLI reference — EC2, S3, IAM, Lambda, ECS, RDS, CloudFormation, and common operations.
🐹
Go Reference
Complete Go (Golang) reference — syntax, types, functions, concurrency, error handling, and common patterns.
💠
PowerShell Reference
Complete PowerShell reference — cmdlets, pipelines, scripting, file operations, remote management, and Active Directory.
💾
Redis Commands
Complete Redis command reference — strings, hashes, lists, sets, sorted sets, pub/sub, transactions, and server management.
🏗️
Terraform Commands
Complete Terraform reference — init, plan, apply, state management, modules, workspaces, and HCL syntax.
⚙️
Ansible Commands
Complete Ansible reference — playbooks, modules, inventory, roles, vault, and ad-hoc commands.

📖 Free, searchable command reference. Bookmark this page for quick access.