Regex Quick Reference
Regular expressions are a powerful pattern-matching language available in nearly every programming language. This cheat sheet focuses on practical usage over theory.
Core Syntax
Anchors
| Pattern | Matches |
|---|---|
^ |
Start of string (or line in multiline mode) |
$ |
End of string (or line in multiline mode) |
\b |
Word boundary |
\B |
Non-word boundary |
/^hello/.test('hello world') // true - starts with hello
/world$/.test('hello world') // true - ends with world
/\bword\b/.test('password') // false - word is not standalone
/\bword\b/.test('a word here') // true
Character Classes
| Pattern | Matches |
|---|---|
. |
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] |
a, b, or c |
[^abc] |
Anything except a, b, or c |
[a-z] |
Any lowercase letter |
[a-zA-Z0-9] |
Alphanumeric |
Quantifiers
| Pattern | Meaning |
|---|---|
* |
0 or more |
+ |
1 or more |
? |
0 or 1 (optional) |
{n} |
Exactly n times |
{n,} |
n or more times |
{n,m} |
Between n and m times |
*? |
0 or more (lazy/non-greedy) |
+? |
1 or more (lazy) |
Greedy vs lazy:
'<b>bold</b>'.match(/<.+>/)[0] // '<b>bold</b>' — greedy (takes as much as possible)
'<b>bold</b>'.match(/<.+?>/)[0] // '<b>' — lazy (takes as little as possible)
Groups and Alternation
| Pattern | Meaning |
|---|---|
(abc) |
Capturing group |
(?:abc) |
Non-capturing group |
(?<name>abc) |
Named capturing group |
| `a | b` |
const match = '2026-05-28'.match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
match.groups.year // '2026'
match.groups.month // '05'
match.groups.day // '28'
Lookarounds (Zero-Width Assertions)
| Pattern | Meaning |
|---|---|
(?=...) |
Positive lookahead — followed by |
(?!...) |
Negative lookahead — not followed by |
(?<=...) |
Positive lookbehind — preceded by |
(?<!...) |
Negative lookbehind — not preceded by |
// Find numbers followed by 'px'
'10px 20em 30px'.match(/\d+(?=px)/g) // ['10', '30']
// Find numbers NOT followed by 'px'
'10px 20em 30px'.match(/\d+(?!px)\d*/g) // ['20']
// Price without currency symbol
'$100'.match(/(?<=\$)\d+/)[0] // '100'
Flags
| Flag | Effect |
|---|---|
g |
Global — find all matches |
i |
Case-insensitive |
m |
Multiline — ^ and $ match line boundaries |
s |
Dotall — . matches newlines |
u |
Unicode mode |
d |
Generate indices for substrings (ES2022) |
20+ Ready-to-Use Patterns
Email Address
const emailRegex = /^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$/;
emailRegex.test('user@example.com') // true
emailRegex.test('user+tag@sub.co.uk') // true
emailRegex.test('not-an-email') // false
URL
const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)/;
IPv4 Address
const ipv4 = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
ipv4.test('192.168.1.1') // true
ipv4.test('999.0.0.1') // false
Strong Password (8+ chars, uppercase, lowercase, digit, special)
const strongPassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$/;
Date (YYYY-MM-DD)
const isoDate = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
Phone Number (US)
const usPhone = /^(\+1\s?)?\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}$/;
Credit Card Number
const creditCard = /^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})$/;
Hex Color Code
const hexColor = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
hexColor.test('#fff') // true
hexColor.test('#3a9f2e') // true
hexColor.test('#gggggg') // false
Slug (URL-safe string)
const slug = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
slug.test('my-article-title') // true
slug.test('my_article') // false
Extract All URLs from Text
const text = 'Visit https://example.com or http://test.org for more.';
const urls = text.match(/https?:\/\/[^\s]+/g);
// ['https://example.com', 'http://test.org']
Remove HTML Tags
const html = '<p>Hello <b>World</b></p>';
html.replace(/<[^>]+>/g, ''); // 'Hello World'
Trim Whitespace
str.replace(/^\s+|\s+$/g, '') // same as str.trim()
str.replace(/\s+/g, ' ') // collapse multiple spaces to one
Extract Numbers from String
'Price: $29.99, Qty: 3'.match(/\d+\.?\d*/g) // ['29.99', '3']
JavaScript Regex Methods
// test() — returns boolean
/\d+/.test('abc123') // true
// match() — returns array of matches (or null)
'hello world'.match(/\w+/g) // ['hello', 'world']
// replace() / replaceAll()
'foo bar foo'.replace(/foo/g, 'baz') // 'baz bar baz'
// split()
'a,b,,c'.split(/,+/) // ['a', 'b', 'c']
// exec() — returns match with index
const re = /\d+/g;
let m;
while ((m = re.exec('a1b22c333')) !== null) {
console.log(m[0], 'at', m.index);
}
Frequently Asked Questions
Q: How do I make a regex case-insensitive?
Add the i flag: /pattern/i or new RegExp('pattern', 'i').
Q: Why does my regex with g flag behave strangely when called repeatedly?
The g flag maintains lastIndex state on the regex object. If you reuse the same regex object, exec() starts from where it left off. Either create a new regex each time, or reset re.lastIndex = 0.
Q: What's the difference between match() and matchAll()?
match() with g returns all matches but no capture groups. matchAll() returns an iterator of all match objects including capture groups.
const str = 'test1 test2';
[...str.matchAll(/test(\d)/g)].map(m => m[1]) // ['1', '2']
→ Test and debug your regex patterns live with the Regex Tester.