Regexdrill

Patterns for common data Updated November 2026

Regex for phone number

For US numbers, ^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ matches 555-123-4567, (555) 123-4567, 555.123.4567 and 5551234567. For international input, strip everything except digits and the plus sign first, then check ^\+?[1-9]\d{6,14}$ (E.164 allows up to 15 digits).

Syntax

/^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/

Examples

["(555) 123-4567", "555.123.4567", "5551234567", "55-1234"].map(s => /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/.test(s))

Result: [true, true, true, false]

"+1 (555) 123-4567".replace(/[^\d+]/g, "")

Result: "+15551234567"

Normalize first: keep only digits and the plus sign.

/^\+?[1-9]\d{6,14}$/.test("+15551234567")

Result: true

E.164 shape: optional plus, no leading zero, 7 to 15 digits.

"555-123-4567 x89".match(/^(\d{3})-(\d{3})-(\d{4})(?: ?(?:x|ext\.?) ?(\d+))?$/)

Result: ["555-123-4567 x89", "555", "123", "4567", "89"]

An optional group for an extension; group 4 is undefined when there is none.

Common mistakes

  • \d{10} rejects every formatted number; allow optional separators with [-.\s]? or strip them before testing.
  • Testing without ^ and $ accepts a number buried in a longer string; anchor the pattern.
  • Allowing an optional leading 1 (1?) and also an optional plus without deciding what you store; pick one canonical format such as E.164.
  • Rejecting valid foreign numbers with a US-only pattern; if users are international, validate length and digits only, or use a library such as libphonenumber.

Practice Regex for phone number

These drills check your answer on live data, free, no account needed.

  • US phone number formatsThe whole line must match. Accept (555) 123-4567, or ten digits split 3, 3 and 4 by the same separator throughout: a hyphen, a dot, or nothing.
  • Optional group: phone extensionThe whole line must match. Match a number like 555-0142, optionally followed by a space, ext, a space and 1 to 4 digits.
  • Keep only the digitsRemove every character that is not a digit, so phone numbers in any format become plain digits.

Questions

What is the regex for a 10 digit phone number?

^\d{10}$ for digits only. If separators are allowed, use ^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ or strip non-digits first and then test ^\d{10}$.

How do I validate international phone numbers with regex?

Normalize to digits and an optional plus, then test ^\+?[1-9]\d{6,14}$. Country-specific rules (area codes, lengths) are beyond a regex; libphonenumber handles those.

Should I use a regex for phone numbers at all?

A regex is fine for shape checks and for cleaning input. It cannot tell you that a number is assigned or reachable, so keep the rule loose and confirm with an SMS or call if it matters.

Related

  • Regex for numbers onlyThe ^\d+$ pattern for digits-only strings, stripping everything that is not a digit, extracting numbers from text, and patterns for signed and decimal values.
  • Optional with ?How ? makes a character optional, making a whole sequence optional with a group, what the capture holds when the group is absent, and the ? that means lazy.
  • Regex for ZIP codeZIP code regex for US 5-digit and ZIP+4 formats, plus patterns for UK, Canadian and German postal codes, with examples and the common anchoring mistake.
  • Escaping special charactersThe regex metacharacters that must be escaped, how to match a literal dot, dollar, plus or slash, what changes inside brackets, and how to escape user input.

Previous: Regex for emailNext: Regex for date YYYY-MM-DD