Phone Regex Tester
Test and validate phone number patterns with regular expressions. Handle various international formats and separators.
Phone Number Formats
| Format | Example | Pattern |
|---|---|---|
| US (plain) | 5551234567 | \d{10} |
| US (dashes) | 555-123-4567 | \d{3}-\d{3}-\d{4} |
| US (parens) | (555) 123-4567 | \(\d{3}\) \d{3}-\d{4} |
| International | +1-555-123-4567 | \+\d{1,3}-\d{3}-\d{3}-\d{4} |
| E.164 | +15551234567 | \+[1-9]\d{1,14} |
Flexible Phone Patterns
``javascript
// Accept multiple formats
const flexiblePhone = /^\+?1?[-. ]?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}$/;
// Test various formats
const validNumbers = [
'5551234567',
'555-123-4567',
'(555) 123-4567',
'555.123.4567',
'1-555-123-4567',
'+1 555 123 4567'
];
validNumbers.every(n => flexiblePhone.test(n)); // true
// Extract and normalize
function normalizePhone(phone) {
const digits = phone.replace(/\D/g, '');
if (digits.length === 10) {
return (${digits.slice(0,3)}) ${digits.slice(3,6)}-${digits.slice(6)};
}
if (digits.length === 11 && digits[0] === '1') {
return +1 (${digits.slice(1,4)}) ${digits.slice(4,7)}-${digits.slice(7)};
}
return phone;
}
normalizePhone('5551234567'); // "(555) 123-4567"
normalizePhone('15551234567'); // "+1 (555) 123-4567"
`
E.164 International Format
`javascript
// E.164: +[country code][number]
// Max 15 digits total, no separators
const e164Pattern = /^\+[1-9]\d{1,14}$/;
// Country code patterns
const countryCodes = {
US: /^\+1\d{10}$/, // +1 followed by 10 digits
UK: /^\+44\d{10}$/, // +44 followed by 10 digits
AU: /^\+61\d{9}$/, // +61 followed by 9 digits
DE: /^\+49\d{10,11}$/ // +49 followed by 10-11 digits
};
// Validate with country
function validatePhoneByCountry(phone, country) {
const pattern = countryCodes[country];
return pattern ? pattern.test(phone) : e164Pattern.test(phone);
}
`
Extract Phone Numbers
`javascript
// Find all phone numbers in text
const phonePattern = /(?:\+?1[-. ]?)?\(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4}/g;
const text = "Call 555-123-4567 or (800) 555-0199";
const phones = text.match(phonePattern);
// Result: ["555-123-4567", "(800) 555-0199"]
``