Comprehensive Regex Cheat Sheet: Syntax and Examples
A complete reference guide for regular expressions. Contains all essential patterns, flags, and examples for quick lookup.
Regular expressions can be overwhelming with their terse syntax. This comprehensive cheat sheet provides quick reference for the most common regex patterns and concepts.
Character Classes
. Any character except newline
\d Digit [0-9]
\D Non-digit
\w Word character [a-zA-Z0-9_]
\W Non-word character
\s Whitespace (space, tab, newline)
\S Non-whitespace
[abc] Any of a, b, or c
[^abc] Not a, b, or c
[a-z] Character in range
[a-zA-Z0-9] Letters and digits
Anchors
^ Start of string or line
$ End of string or line
\b Word boundary
\B Non-word boundary
\A Start of string (multiline off)
\Z End of string (multiline off)
Quantifiers
* 0 or more occurrences
+ 1 or more occurrences
? 0 or 1 occurrence
{n} Exactly n occurrences
{n,} n or more occurrences
{n,m} Between n and m occurrences
*? Lazy: as few as possible
+? Lazy: 1 or more, minimal
?: Lazy: 0 or 1
{n,m}? Lazy: n to m, minimal
Groups and Alternation
(abc) Capture group
(?:abc) Non-capturing group
(?<name>abc) Named capture group
(a|b) Alternation: a or b
(?:a|b|c) Non-capturing alternation
Lookahead and Lookbehind
(?=abc) Positive lookahead
(?!abc) Negative lookahead
(?<=abc) Positive lookbehind
(?<!abc) Negative lookbehind
Flags
i Case-insensitive
g Global (find all matches)
m Multiline (^ and $ match line start/end)
s Dot-all (. matches newlines)
x Verbose (ignore whitespace in pattern)
y Sticky (start from lastIndex)
u Unicode
Practical Examples
^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$
Phone Number
^\+?[1-9]\d{1,14}$ // International format
^\d{10}$ // US format
^\d{3}-\d{3}-\d{4}$ // US formatted
URL
https?://[^\s]+
^(https?://)?([\w.-]+\.)+[\w.-]+$
Password (Strong)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Postal Code (US)
^\d{5}(-\d{4})?$
Credit Card
^\d{13,19}$
IPv4 Address
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
Date (YYYY-MM-DD)
^\d{4}-\d{2}-\d{2}$
Time (HH:MM:SS)
^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$
JavaScript Methods
// Test if pattern matches
/pattern/.test(string)
// Find first match
string.match(/pattern/)
// Find all matches
string.match(/pattern/g)
// Replace matches
string.replace(/pattern/, 'replacement')
// Replace all matches
string.replace(/pattern/g, 'replacement')
// Split by pattern
string.split(/pattern/)
// Test with exec
const regex = /pattern/g;
while ((match = regex.exec(string)) !== null) {
// Process match
}
Common Mistakes
// ❌ Forgetting to escape special characters
/test.file/ // . matches any character
// ✓ Correct
/test\.file/
// ❌ Wrong delimiters
/[a-z]]/ // Extra ]
// ✓ Correct
/[a-z]/
// ❌ Forgetting g flag for multiple matches
const matches = string.match(/\d+/); // Only first match
// ✓ Correct
const matches = string.match(/\d+/g); // All matches
Testing Your Regex
function testRegex(pattern, testCases) {
const regex = new RegExp(pattern, 'i');
testCases.forEach(({ input, shouldMatch }) => {
const matches = regex.test(input);
const status = matches === shouldMatch ? '✓' : '✗';
console.log(`${status} ${input}`);
});
}
testRegex('^\\d{3}-\\d{3}-\\d{4}$', [
{ input: '123-456-7890', shouldMatch: true },
{ input: '123-456', shouldMatch: false },
]);
Performance Tips
- Be specific with character classes instead of broad patterns
- Use non-capturing groups (?:) when you don't need to capture
- Place alternatives from most to least specific: (cat|car|c)
- Avoid backtracking with possessive quantifiers
- Test patterns at regex101.com before deployment
Online Tools
- regex101.com: Interactive regex tester with explanations
- regexper.com: Visualize regex patterns
- FormatCraft: Quick testing and validation
This cheat sheet covers the essential regex patterns for web development. Keep it handy for quick reference while building validators, parsers, and text processors.
Average reading pace: 200 words per minute
Share Article
Related Articles
Regex for Email Validation: Building Robust Patterns
Learn how to create reliable email validation patterns using regular expressions. Explore different approaches and their trade-offs.
Regex for Numbers Only: Extracting and Validating Numeric Data
Master regex patterns for working with numbers. Learn to extract, validate, and filter numeric data from strings.
Common Regex Examples: Practical Patterns for Daily Development
Explore practical regex patterns you'll use regularly. Learn phone numbers, URLs, postal codes, and more.