New blogMaking an AI Agent You Can Trust With Money

★ Identity & contact · regex

Regex for US Phone Number

Accepts US and Canadian numbers written as (415) 555-2671, 415-555-2671, 415.555.2671 or with +1. Area codes and exchanges can't start with 0 or 1 under the North American Numbering Plan, so those are rejected.

  • Runs in your browser
  • No signup

Try the us phone number pattern

Read it in plain English (best-effort)

  • ^start of a line
  • (?:start a group (not captured)
  • +the character '+'
  • ?the previous is optional
  • 1the character '1'
  • [ .-]one character from: space, '.', '-'
  • ?the previous is optional
  • )end of group
  • ?the previous is optional
  • (the character '('
  • ?the previous is optional
  • (start capture group 1
  • [2-9]one character from: 2–9
  • [0-9]one character from: 0–9
  • {2}exactly 2 of the previous
  • )end of group
  • )the character ')'
  • ?the previous is optional
  • [ .-]one character from: space, '.', '-'
  • ?the previous is optional
  • (start capture group 2
  • [2-9]one character from: 2–9
  • [0-9]one character from: 0–9
  • {2}exactly 2 of the previous
  • )end of group
  • [ .-]one character from: space, '.', '-'
  • ?the previous is optional
  • (start capture group 3
  • [0-9]one character from: 0–9
  • {4}exactly 4 of the previous
  • )end of group
  • $end of a line

One sample per line works well with the m flag on anchored patterns.

Waiting for a pattern
01

The pattern, piece by piece

/^(?:\+?1[ .-]?)?\(?([2-9][0-9]{2})\)?[ .-]?([2-9][0-9]{2})[ .-]?([0-9]{4})$/
Pattern explanation
PartWhat it does
(?:\+?1[ .-]?)?optional country code 1 or +1
\(?([2-9][0-9]{2})\)?group 1: area code, optionally in parentheses
[ .-]?optional separator
([2-9][0-9]{2})group 2: exchange code
([0-9]{4})group 3: line number
02

What it accepts and rejects

Every sample below is checked automatically in JavaScript and Python. The tester above is preloaded with all of them, one per line.

Should match
Should match
(415) 555-2671✓
415-555-2671✓
415.555.2671✓
4155552671✓
+1 415 555 2671✓
1-415-555-2671✓
Should not match
Should not match
(115) 555-2671✗
415-155-2671✗
555-2671✗
415-555-267✗
+44 20 7946 0958✗
415-555-26711✗
03

Copy it into your code

RE2-safeNo lookarounds or backreferences, so the same pattern works in Go's RE2 engine and in Rust's regex crate.

const usPhoneRe = /^(?:\+?1[ .-]?)?\(?([2-9][0-9]{2})\)?[ .-]?([2-9][0-9]{2})[ .-]?([0-9]{4})$/;

export function isValidUsPhone(value) {
  return usPhoneRe.test(value.trim());
}
04

When not to use regex for this

For anything international, or to store numbers properly, use libphonenumber (Google's library, ported to most languages). Normalise to E.164 and store that, not the formatted string.

05

Questions engineers actually ask

Does it allow unbalanced parentheses like (415 555-2671?
Yes. Enforcing balance needs alternation per format; most forms normalise the digits afterwards anyway.
Does it validate that the number exists?
No. Only an SMS or call verification proves a number is real and reachable.
What is catastrophic backtracking?
Some patterns, usually nested quantifiers like (a+)+, make a backtracking engine try an exponential number of paths on input that almost matches. The tester stops any run after 300ms, so a bad pattern can't freeze your tab. In production, that same pattern can take down a server (a ReDoS attack).
06

More identity & contact patterns

07

More from the workbench

Fine print

Tools: on the house.
Engineer: by the hour.

The regex was free. Code that doesn't need one is the upgrade: typed schemas, validated once at the edge, trusted everywhere after.

I'm Himanshu, an AI and full-stack engineer. I build MVPs, AI agents and the growth plumbing around them for early-stage startups.