RegexBuilder · regex answers
Regex for a US phone number
/^(?:\(([2-9][0-8]\d)\)|([2-9][0-8]\d))[-.\s]?([2-9]\d{2})[-.\s]?(\d{4})$/US numbering has rules regexes can enforce: an area code never starts with 0 or 1, and its second digit is never 9; an exchange code never starts with 0 or 1. This accepts the four separator styles people actually type and captures the three parts so you can normalise them.
What each part does
| \( … \)|… | the area code either fully in parentheses or with none at all — never half |
| [2-9][0-8]\d | area code — first digit 2-9, second 0-8, third anything |
| [-.\s]? | an optional single separator: hyphen, dot or space |
| [2-9]\d{2} | exchange code — never starts with 0 or 1 |
| \d{4} | the four-digit subscriber number |
Tested against real input
Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.
| Input | Result | Matched |
|---|---|---|
| (415) 555-2671 | matches | (415) 555-2671 |
| 415-555-2671 | matches | 415-555-2671 |
| 415.555.2671 | matches | 415.555.2671 |
| 4155552671 | matches | 4155552671 |
| (415 555-2671 | no match | — |
| 115-555-2671 | no match | — |
| 415-055-2671 | no match | — |
| +1 415 555 2671 | no match | — |
The mistake to avoid
Anchoring with ^...$ and then feeding it a number that still carries a +1 country code. Strip the country code first, or the whole match fails silently.
Try it on your own text
Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.
Open RegexBuilderOther regex answers
- Regex for an email address
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID
- Regex for a hex colour code