Mastering Regular Expressions 2026 - Complete Pattern Guide with Examples
From email, URL, and phone number validation to complex text parsing. Learn practical regex patterns that you can use immediately with working code examples.
Toolypet Team
Development Team
Mastering Regular Expressions 2026
"Regular expressions are difficult" - that's true. But once you learn them, you can reduce text processing time by 90%.
This guide covers the most commonly used regex patterns in practice with examples.
Regex Basics
Basic Syntax
| Pattern | Description | Example |
|---|---|---|
. | Any single character | a.c -> "abc", "a1c" |
* | 0 or more | ab*c -> "ac", "abc", "abbc" |
+ | 1 or more | ab+c -> "abc", "abbc" |
? | 0 or 1 | colou?r -> "color", "colour" |
^ | Start of string | ^Hello |
$ | End of string | world$ |
\d | Digit [0-9] | \d{3} -> "123" |
\w | Word character [a-zA-Z0-9_] | \w+ |
\s | Whitespace character | \s+ |
Character Classes
[abc] - One of a, b, c
[^abc] - Except a, b, c
[a-z] - a through z
[A-Z] - A through Z
[0-9] - 0 through 9
[a-zA-Z] - All letters
Quantifiers
{n} - Exactly n
{n,} - n or more
{n,m} - Between n and m
* - 0 or more ({0,})
+ - 1 or more ({1,})
? - 0 or 1 ({0,1})
Groups and Capturing
(abc) - Capture group
(?:abc) - Non-capturing group
(?<name>abc) - Named group
\1 - Backreference to first group
Practical Patterns: Validation
Email Address
// Basic pattern
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
// Test
emailRegex.test('user@example.com'); // true
emailRegex.test('invalid-email'); // false
Pattern Analysis:
^ - Start
[a-zA-Z0-9._%+-]+ - Local part (1+ chars)
@ - @ symbol
[a-zA-Z0-9.-]+ - Domain (1+ chars)
\. - . (escaped)
[a-zA-Z]{2,} - TLD (2+ chars)
$ - End
URL
const urlRegex = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/;
// Test
urlRegex.test('https://example.com'); // true
urlRegex.test('https://sub.example.com/path'); // true
urlRegex.test('invalid url'); // false
Phone Number (US Format)
// US phone numbers
const usPhoneRegex = /^(\+1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/;
// Test
usPhoneRegex.test('(555) 123-4567'); // true
usPhoneRegex.test('555-123-4567'); // true
usPhoneRegex.test('+1 555 123 4567'); // true
Password Strength
// Minimum 8 chars, upper/lowercase, number, special char
const strongPassword = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
// Pattern analysis
// (?=.*[a-z]) - At least 1 lowercase (lookahead)
// (?=.*[A-Z]) - At least 1 uppercase
// (?=.*\d) - At least 1 digit
// (?=.*[@$!%*?&]) - At least 1 special char
// {8,} - 8 or more characters
Credit Card Number
// Visa
const visaRegex = /^4[0-9]{12}(?:[0-9]{3})?$/;
// MasterCard
const mastercardRegex = /^5[1-5][0-9]{14}$/;
// All cards (with optional hyphens)
const cardRegex = /^\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}$/;
Practical Patterns: Text Extraction
HTML Tag Content Extraction
const html = '<div class="title">Hello World</div>';
// Extract tag content
const contentRegex = /<div[^>]*>(.*?)<\/div>/;
const match = html.match(contentRegex);
console.log(match[1]); // "Hello World"
// Remove all tags
const noTags = html.replace(/<[^>]*>/g, '');
console.log(noTags); // "Hello World"
Extract Domain from URL
const url = 'https://www.example.com/path/page.html';
const domainRegex = /^(?:https?:\/\/)?(?:www\.)?([^/]+)/;
const domain = url.match(domainRegex)[1];
console.log(domain); // "example.com"
Log File Parsing
const logLine = '[2026-02-21 14:30:45] ERROR: Connection timeout at module.js:42';
const logRegex = /\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\] (\w+): (.+) at (.+):(\d+)/;
const [, datetime, level, message, file, line] = logLine.match(logRegex);
console.log({
datetime, // "2026-02-21 14:30:45"
level, // "ERROR"
message, // "Connection timeout"
file, // "module.js"
line, // "42"
});
CSV Parsing (Comma-separated)
const csvLine = 'John,"Doe, Jr.",30,"New York, NY"';
// Split by comma (ignore commas inside quotes)
const csvRegex = /(?:^|,)("(?:[^"]*(?:""[^"]*)*)"|[^,]*)/g;
const fields = [];
let match;
while ((match = csvRegex.exec(csvLine)) !== null) {
fields.push(match[1].replace(/^"|"$/g, '').replace(/""/g, '"'));
}
console.log(fields); // ["John", "Doe, Jr.", "30", "New York, NY"]
Practical Patterns: Search and Replace
Phone Number Formatting
const phone = '5551234567';
// Add hyphens
const formatted = phone.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
console.log(formatted); // "555-123-4567"
Number Formatting
const amount = '1234567890';
// Thousand separators
const formatted = amount.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
console.log(formatted); // "1,234,567,890"
Masking
// Email masking
const email = 'username@example.com';
const masked = email.replace(/(.{2})(.*)(@.*)/, '$1***$3');
console.log(masked); // "us***@example.com"
// Phone number masking
const phone = '555-123-4567';
const maskedPhone = phone.replace(/(\d{3})-(\d{3})-(\d{4})/, '$1-***-$3');
console.log(maskedPhone); // "555-***-4567"
Whitespace Cleanup
const text = ' Hello World ';
// Trim leading/trailing whitespace
const trimmed = text.replace(/^\s+|\s+$/g, '');
// Normalize multiple spaces to one
const normalized = text.replace(/\s+/g, ' ').trim();
console.log(normalized); // "Hello World"
Advanced Features
Lookahead
// Positive Lookahead: (?=...)
// Match "foo" only if followed by "bar"
const regex = /foo(?=bar)/;
'foobar'.match(regex); // ["foo"]
'foobaz'.match(regex); // null
// Negative Lookahead: (?!...)
// Match "foo" only if NOT followed by "bar"
const regex2 = /foo(?!bar)/;
'foobaz'.match(regex2); // ["foo"]
'foobar'.match(regex2); // null
Lookbehind
// Positive Lookbehind: (?<=...)
// Numbers preceded by "$"
const priceRegex = /(?<=\$)\d+/g;
'$100 and $200'.match(priceRegex); // ["100", "200"]
// Negative Lookbehind: (?<!...)
// Numbers NOT preceded by "$"
const nonPriceRegex = /(?<!\$)\d+/g;
'$100 and 200'.match(nonPriceRegex); // ["200"]
Named Groups
const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = '2026-02-21'.match(dateRegex);
console.log(match.groups.year); // "2026"
console.log(match.groups.month); // "02"
console.log(match.groups.day); // "21"
Non-Greedy
const html = '<div>Hello</div><div>World</div>';
// Greedy (default): Match as much as possible
/<div>.*<\/div>/.exec(html)[0];
// "<div>Hello</div><div>World</div>"
// Non-Greedy: Match as little as possible
/<div>.*?<\/div>/.exec(html)[0];
// "<div>Hello</div>"
Language-Specific Implementation
JavaScript
// Creation methods
const regex1 = /pattern/flags;
const regex2 = new RegExp('pattern', 'flags');
// Methods
regex.test(str); // boolean
str.match(regex); // array or null
str.replace(regex, replacement);
str.split(regex);
regex.exec(str); // detailed match info
// Flags
// g: Global search
// i: Case insensitive
// m: Multiline
// s: dotAll (. matches newline)
// u: Unicode
Python
import re
# Compile
pattern = re.compile(r'pattern', re.IGNORECASE)
# Methods
re.match(pattern, string) # Match from start
re.search(pattern, string) # Search entire string
re.findall(pattern, string) # List all matches
re.sub(pattern, repl, string) # Replace
# Example
emails = re.findall(r'[\w.+-]+@[\w.-]+\.\w+', text)
cleaned = re.sub(r'\s+', ' ', text).strip()
Go
import "regexp"
// Compile
re := regexp.MustCompile(`\d+`)
// Methods
re.MatchString(s) // bool
re.FindString(s) // First match
re.FindAllString(s, -1) // All matches
re.ReplaceAllString(s, r) // Replace
// Example
numbers := re.FindAllString("a1b2c3", -1)
// ["1", "2", "3"]
Performance Optimization
Reuse Compiled Regex
// Slow: Compiles every time
function validate(email) {
return /^[\w.+-]+@[\w.-]+\.\w+$/.test(email);
}
// Fast: Compile once
const emailRegex = /^[\w.+-]+@[\w.-]+\.\w+$/;
function validate(email) {
return emailRegex.test(email);
}
Use Non-Capturing Groups
// Unnecessary capture
const regex = /(https?):\/\/(www\.)?(.+)/;
// Non-capturing for unneeded groups
const regex = /(?:https?):\/\/(?:www\.)?(.+)/;
Avoid Catastrophic Backtracking
// Dangerous: (a+)+ pattern
const bad = /^(a+)+$/;
'aaaaaaaaaaaaaaaaaaaaaa!'.match(bad); // Very slow
// Safe: Restructure the pattern
const good = /^a+$/;
Debugging Tips
Using Regex Tester
- Enter pattern
- Enter test string
- Check highlighted matches
- Verify capture groups
Step-by-Step Validation
// Validate complex patterns step by step
const parts = [
'^', // Start
'[a-zA-Z0-9._%+-]+', // Local part
'@', // @
'[a-zA-Z0-9.-]+', // Domain
'\\.', // .
'[a-zA-Z]{2,}', // TLD
'$', // End
];
const emailRegex = new RegExp(parts.join(''));
FAQ
Q1: What's the difference between . and \.?
A: . matches any character (wildcard), \. matches a literal dot.
Q2: What's the difference between * and +?
A: * matches 0 or more, + matches 1 or more.
ab*c-> "ac", "abc", "abbc"ab+c-> "abc", "abbc" ("ac" doesn't match)
Q3: How do I ignore case?
A: Use the i flag.
- JavaScript:
/pattern/i - Python:
re.IGNORECASE
Q4: How do I make . match newlines?
A: Use the s flag (dotAll).
- JavaScript:
/pattern/s - Python:
re.DOTALL
Q5: Which characters need escaping?
A: \ ^ $ . | ? * + ( ) [ ] { } - escape these with \.
Conclusion
Regex essentials:
- Basic syntax:
.,*,+,?,[],() - Meta characters:
\d,\w,\s,^,$ - Quantifiers:
{n},{n,},{n,m} - Advanced features: Lookahead, Lookbehind, Named Groups
Practice is key. Test in real-time at Regex Tester.
Related Tools
| Tool | Purpose |
|---|---|
| Regex Tester | Regex testing and debugging |
| JSON Formatter | JSON formatting |
About the Author
Toolypet Team
Development Team
The Toolypet Team creates free, privacy-focused web tools for developers and designers. All tools run entirely in your browser with no data sent to servers.