Regular expressions are powerful but hard to remember. This guide gives you tested JavaScript regex patterns for the most common validation tasks. Copy them directly into your code.
Every pattern below works with JavaScript's native RegExp. You can test them instantly using our free regex tester before adding them to your project.
Quick Navigation
1. Email Validation
The most common regex use case. This pattern covers standard email formats.
Pattern:
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;Usage:
emailRegex.test('user@example.com'); // true
emailRegex.test('name.last@company.co'); // true
emailRegex.test('invalid-email'); // false
emailRegex.test('missing@tld'); // falseWhat it matches:
- Letters, numbers, dots, underscores, percent, plus, hyphen before @
- Domain name with letters, numbers, dots, hyphens
- TLD with 2 or more letters
Limitations: This pattern does not validate every edge case in RFC 5322. For production apps handling critical email validation, consider a dedicated library or server-side verification.
2. Phone Number Validation
Phone formats vary by country. This pattern handles common US formats.
Pattern (US):
const phoneRegex = /^\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})$/;Usage:
phoneRegex.test('(555) 123-4567'); // true
phoneRegex.test('555-123-4567'); // true
phoneRegex.test('555.123.4567'); // true
phoneRegex.test('5551234567'); // true
phoneRegex.test('555-12-4567'); // false (wrong grouping)With capture groups:
const match = '(555) 123-4567'.match(phoneRegex);
// match[1] = '555' (area code)
// match[2] = '123' (exchange)
// match[3] = '4567' (subscriber)International format (E.164):
const intlPhoneRegex = /^\+[1-9]\d{1,14}$/;
intlPhoneRegex.test('+14155551234'); // true3. URL Validation
Validate URLs with optional protocol and query strings.
Pattern (with protocol):
const urlRegex = /^https?:\/\/[a-zA-Z0-9][-a-zA-Z0-9]*(\.[a-zA-Z0-9][-a-zA-Z0-9]*)+(:\d+)?(\/[^\s]*)?$/;Usage:
urlRegex.test('https://example.com'); // true
urlRegex.test('http://sub.example.com/path'); // true
urlRegex.test('https://example.com:8080/page'); // true
urlRegex.test('ftp://example.com'); // false (not http/https)
urlRegex.test('example.com'); // false (no protocol)Simpler pattern (protocol optional):
const simpleUrlRegex = /^(https?:\/\/)?[\w.-]+\.[a-zA-Z]{2,}(\/\S*)?$/;
simpleUrlRegex.test('example.com'); // true
simpleUrlRegex.test('www.example.com'); // true4. Date Format Validation
Different regions use different date formats. Here are patterns for each.
ISO format (YYYY-MM-DD):
const isoDateRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
isoDateRegex.test('2026-01-08'); // true
isoDateRegex.test('26-01-08'); // false (year needs 4 digits)US format (MM/DD/YYYY):
const usDateRegex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
usDateRegex.test('01/08/2026'); // true
usDateRegex.test('1/8/2026'); // trueEU format (DD/MM/YYYY or DD.MM.YYYY):
const euDateRegex = /^(\d{1,2})[\/\.](\d{1,2})[\/\.](\d{4})$/;
euDateRegex.test('08/01/2026'); // true
euDateRegex.test('08.01.2026'); // trueExtract date parts with capture groups:
const match = '2026-01-08'.match(isoDateRegex);
// match[1] = '2026' (year)
// match[2] = '01' (month)
// match[3] = '08' (day)Note: These patterns validate format only, not whether the date is valid (like February 30). Use new Date() parsing for logical validation.
5. Decimal Number Validation
Match integers, decimals, and numbers with optional signs.
Basic decimal:
const decimalRegex = /^-?\d+(\.\d+)?$/;
decimalRegex.test('42'); // true
decimalRegex.test('-42'); // true
decimalRegex.test('3.14'); // true
decimalRegex.test('-3.14'); // true
decimalRegex.test('.5'); // false (needs leading digit)
decimalRegex.test('3.'); // false (needs digits after decimal)Currency format (two decimal places):
const currencyRegex = /^\d+\.\d{2}$/;
currencyRegex.test('19.99'); // true
currencyRegex.test('19.9'); // false (needs exactly 2 decimals)Scientific notation:
const scientificRegex = /^-?\d+(\.\d+)?([eE][+-]?\d+)?$/;
scientificRegex.test('1.5e10'); // true
scientificRegex.test('-2.5E-3'); // true6. Password Strength Validation
Enforce password requirements with a single pattern.
Minimum 8 chars, 1 uppercase, 1 lowercase, 1 number:
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;
passwordRegex.test('Password1'); // true
passwordRegex.test('password1'); // false (no uppercase)
passwordRegex.test('PASSWORD1'); // false (no lowercase)
passwordRegex.test('Password'); // false (no number)
passwordRegex.test('Pass1'); // false (too short)Add special character requirement:
const strongPasswordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$/;
strongPasswordRegex.test('Password1!'); // true
strongPasswordRegex.test('Password1'); // false (no special char)How lookaheads work:
(?=.*[a-z])- Must contain at least one lowercase letter(?=.*[A-Z])- Must contain at least one uppercase letter(?=.*\d)- Must contain at least one digit(?=.*[!@#$%^&*])- Must contain at least one special character.{8,}- Must be at least 8 characters total
7. Test for Empty String
Simple patterns for checking empty or whitespace-only strings.
Exactly empty:
const emptyRegex = /^$/;
emptyRegex.test(''); // true
emptyRegex.test(' '); // false
emptyRegex.test('text'); // falseEmpty or whitespace only:
const blankRegex = /^\s*$/;
blankRegex.test(''); // true
blankRegex.test(' '); // true
blankRegex.test('\t\n'); // true
blankRegex.test('text'); // falsePractical usage:
function isBlank(str) {
return /^\s*$/.test(str);
}
function hasContent(str) {
return /\S/.test(str);
}8. Using Capture Groups
Capture groups let you extract specific parts of a match. Wrap any part of your pattern in parentheses.
Basic capture:
const dateRegex = /(\d{4})-(\d{2})-(\d{2})/;
const match = '2026-01-08'.match(dateRegex);
console.log(match[0]); // '2026-01-08' (full match)
console.log(match[1]); // '2026' (first group)
console.log(match[2]); // '01' (second group)
console.log(match[3]); // '08' (third group)Named capture groups (ES2018+):
const namedDateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = '2026-01-08'.match(namedDateRegex);
console.log(match.groups.year); // '2026'
console.log(match.groups.month); // '01'
console.log(match.groups.day); // '08'Multiple matches with matchAll:
const tagRegex = /<(\w+)>/g;
const html = '<div><span><p>';
const matches = [...html.matchAll(tagRegex)];
matches.forEach(m => console.log(m[1])); // 'div', 'span', 'p'Replace with capture groups:
// Swap first and last name
const name = 'John Smith';
const swapped = name.replace(/(\w+) (\w+)/, '$2, $1');
console.log(swapped); // 'Smith, John'9. IP Address Validation
Match IPv4 and IPv6 addresses.
IPv4 (simple):
const ipv4SimpleRegex = /^(\d{1,3}\.){3}\d{1,3}$/;
ipv4SimpleRegex.test('192.168.1.1'); // true
ipv4SimpleRegex.test('999.999.999.999'); // true (format valid, values invalid)IPv4 (strict, 0-255):
const ipv4StrictRegex = /^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$/;
ipv4StrictRegex.test('192.168.1.1'); // true
ipv4StrictRegex.test('255.255.255.255'); // true
ipv4StrictRegex.test('256.1.1.1'); // false (256 > 255)IPv6 (simplified):
const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
ipv6Regex.test('2001:0db8:85a3:0000:0000:8a2e:0370:7334'); // true10. Username Validation
Common username rules: alphanumeric, underscores, length limits.
Basic username (3-16 chars, alphanumeric + underscore):
const usernameRegex = /^[a-zA-Z0-9_]{3,16}$/;
usernameRegex.test('john_doe'); // true
usernameRegex.test('user123'); // true
usernameRegex.test('ab'); // false (too short)
usernameRegex.test('user@name'); // false (invalid char)Must start with letter:
const strictUsernameRegex = /^[a-zA-Z][a-zA-Z0-9_]{2,15}$/;
strictUsernameRegex.test('john_doe'); // true
strictUsernameRegex.test('123user'); // false (starts with number)
strictUsernameRegex.test('_hidden'); // false (starts with underscore)Quick Reference: JavaScript Regex Methods
| Method | Returns | Use Case |
|---|---|---|
regex.test(string) | true/false | Validation, conditionals |
string.match(regex) | Array or null | Extract matches |
string.matchAll(regex) | Iterator | Multiple matches with groups |
string.replace(regex, replacement) | New string | Find and replace |
string.search(regex) | Index or -1 | Find position |
string.split(regex) | Array | Split by pattern |
Common Regex Flags
| Flag | Name | Effect |
|---|---|---|
g | Global | Find all matches, not just first |
i | Case-insensitive | Ignore uppercase/lowercase |
m | Multiline | ^ and $ match line starts/ends |
s | DotAll | . matches newlines too |
u | Unicode | Full Unicode support |
const regex = /hello/gi; // global + case-insensitive
'Hello hello HELLO'.match(regex); // ['Hello', 'hello', 'HELLO']Test Your Patterns
Before adding regex to your code, test it with real data. Our JavaScript regex tester shows matches highlighted in real-time with capture group details.
All processing happens in your browser, so you can safely test patterns against sensitive data like API keys or log files.