1. Regex Basics
Regular expressions (regex) are patterns used to match character combinations in strings. They're used for validation, search/replace, and text extraction.
Key Concepts:
- Literal characters match themselves:
catmatches "cat" - Special characters have special meaning:
. * + ? ^ $ [ ] { } ( ) | \ - Escape special chars with backslash:
\.matches a literal period
2. Character Classes
| Pattern | Description | Example |
|---|---|---|
. |
Any character (except newline) | a.c → abc, a1c, a@c |
\d |
Digit [0-9] | \d{3} → 123, 456 |
\D |
Non-digit | \D+ → abc, @#$ |
\w |
Word character [a-zA-Z0-9_] | \w+ → hello_123 |
\W |
Non-word character | \W → @, #, space |
\s |
Whitespace (space, tab, newline) | a\sb → a b |
\S |
Non-whitespace | \S+ → hello |
[abc] |
Any of a, b, or c | [aeiou] → vowels |
[^abc] |
Not a, b, or c | [^0-9] → non-digits |
[a-z] |
Range a to z | [A-Za-z] → letters |
3. Quantifiers
| Pattern | Description | Example |
|---|---|---|
* |
0 or more | a* → "", a, aa, aaa |
+ |
1 or more | a+ → a, aa, aaa (not "") |
? |
0 or 1 (optional) | colou?r → color, colour |
{n} |
Exactly n times | \d{4} → 2024 |
{n,} |
n or more times | \d{2,} → 12, 123, 1234 |
{n,m} |
Between n and m times | \d{2,4} → 12, 123, 1234 |
*? |
Non-greedy * (match minimum) | <.*?> → first tag only |
+? |
Non-greedy + | \d+? → single digit |
4. Anchors & Boundaries
| Pattern | Description | Example |
|---|---|---|
^ |
Start of string/line | ^Hello → starts with Hello |
$ |
End of string/line | world$ → ends with world |
\b |
Word boundary | \bcat\b → "cat" not "cats" |
\B |
Non-word boundary | \Bcat → "scat" not "cat" |
5. Groups & Lookaround
| Pattern | Description | Example |
|---|---|---|
(abc) |
Capture group | (ha)+ → haha |
(?:abc) |
Non-capture group | (?:ha)+ → match but don't capture |
(?=abc) |
Positive lookahead | \d(?=px) → digit before px |
(?!abc) |
Negative lookahead | \d(?!px) → digit not before px |
(?<=abc) |
Positive lookbehind | (?<=\$)\d+ → digits after $ |
(?<!abc) |
Negative lookbehind | (?<!\$)\d+ → digits not after $ |
a|b |
Alternation (or) | cat|dog → cat or dog |
\1 |
Backreference | (.)\1 → repeated char (aa, bb) |
6. Ready-to-Use Patterns
Copy these patterns directly into your code. Click "Test" to try them in our Regex Tester.
📧 Email & Contact
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Matches: user@example.com, name.surname@company.co.uk
^\+?[1-9]\d{1,14}$
E.164 format: +14155551234, +442071234567
^\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})$
Matches: (555) 123-4567, 555-123-4567, 555.123.4567
🌐 URLs & Web
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$
^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$
Matches: 192.168.1.1, 10.0.0.255 (validates 0-255 range)
^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$
Matches: #ff5733, #FFF, abc123
🔐 Security & Passwords
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Min 8 chars with uppercase, lowercase, number, special char
^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$
📅 Dates & Numbers
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
ISO 8601 format: 2024-01-17
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})$
Visa, Mastercard, Amex (basic validation only)
^\d{5}(-\d{4})?$
5-digit or ZIP+4: 12345, 12345-6789
7. Regex in JavaScript
// Create regex
const regex = /pattern/flags;
const regex2 = new RegExp('pattern', 'flags');
// Flags
// g = global (find all matches)
// i = case-insensitive
// m = multiline (^ and $ match line start/end)
// Test if matches
regex.test('string'); // returns true/false
// Find matches
'string'.match(/pattern/g); // returns array or null
[...'string'.matchAll(/pattern/g)]; // all matches with groups
// Replace
'hello world'.replace(/world/, 'regex'); // 'hello regex'
'hello world'.replace(/o/g, '0'); // 'hell0 w0rld'
// Split
'a,b;c'.split(/[,;]/); // ['a', 'b', 'c']
// Extract groups
const match = 'John Doe'.match(/(\w+) (\w+)/);
// match[0] = 'John Doe' (full match)
// match[1] = 'John' (first group)
// match[2] = 'Doe' (second group)