Regex for Phone Number Matching and Validation
Regex patterns for matching phone numbers in international, US, European, and E.164 formats. Handles optional country codes, area codes, and various separators.
Common Patterns
Detailed Explanation
Phone Number Matching with Regex
Phone number formats vary enormously across countries and user preferences. Here are patterns from simple to comprehensive.
US Phone Numbers
(?:\+?1[-.]?)?\(?\d{3}\)?[-.]?\d{3}[-.]?\d{4}
Matches:
555-123-4567(555) 123-4567+1-555-123-45675551234567
International Format (E.164)
\+[1-9]\d{1,14}
E.164 is the international telephone numbering standard:
- Starts with
+followed by country code - 1 to 15 digits total
- No separators
Flexible International Pattern
\+?\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}
This handles most common formats with various separators (dash, dot, space) and optional parentheses around area codes.
Named Group Extraction
(?<country>\+\d{1,3})?[-.\s]?\(?(?<area>\d{3})\)?[-.\s]?(?<prefix>\d{3})[-.\s]?(?<line>\d{4})
Practical Considerations
- Phone numbers are notoriously difficult to validate with regex alone
- International formats have wildly different lengths and structures
- Consider using a library like
libphonenumberfor production validation - For simple form validation, accept digits and common separators, then normalize server-side
- Strip non-digit characters before storage:
phone.replace(/\D/g, "")
Use Case
You are building a contact form that accepts phone numbers in various formats, extracting phone numbers from text or documents, or normalizing phone number input to a consistent format for database storage.