Regex Tester & Debugger→Specialized Version
πŸ”

Phone Number Regex Tester

Phone Number Regex Tester

//gm
Flags:
Examples:
+1 (555) 123-4567 555-123-4567 5551234567 555-1234
#MatchIndexGroups
1+1 (555) 123-45670555, 123, 4567
2555-123-456718555, 123, 4567
3555123456731555, 123, 4567

Phone Number Regex Tester

Test and validate phone patterns with this specialized regex tester. Includes tested patterns and real-time matching.

Recommended Phone Pattern

``regex ^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$ `

Test Examples

Valid matches:

  • (555) 123-4567
  • 555-123-4567
  • +1 555 123 4567
  • 5551234567
Invalid (should not match):
  • 555-123
  • 123-456-78901
  • abc-def-ghij

Pattern Explanation

Matches US phone numbers with optional country code, parentheses, and separators

Alternative Patterns

1. ^\d{10}$ 2. ^\(\d{3}\) \d{3}-\d{4}$

How to Use

1. The pattern above is preloadedβ€”or enter your own 2. Add test strings to validate 3. See real-time match highlights 4. Copy the pattern for your code

Regex Quick Reference

SymbolMeaningExample
\dAny digit\d{3} matches "123"
\wWord character\w+ matches "hello"
+One or morea+ matches "aaa"
*Zero or morea* matches "" or "aaa"
?Optionalcolou?r matches "color"
^Start of string^Hello
$End of stringworld$
[abc]Character class[aeiou] matches vowels
(ab)Alternation(catdog) matches either

The Pattern

`regex ^\+?1?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})$ `

Flags: gm β€” m makes ^ and $ match at each line break, so the pattern can be tested against a list.

Broken Down

PartWhat it does
\+?1?optional country code
[-. (]*any separator style β€” dashes, dots, spaces, an opening paren
(\d{3})area code, captured
(\d{4})$the final four digits, anchored

Tested Against Real Input

InputResult
+1 (555) 123-4567βœ… matches
555-123-4567βœ… matches
5551234567βœ… matches
555-1234❌ no match

The Important Caveat

This matches North American numbers. International formats vary far too much for one pattern β€” use libphonenumber if you need to accept the world.

Using It

`javascript const pattern = /^\+?1?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})$/gm;

// Test a single value β€” reset lastIndex first if the pattern is global pattern.lastIndex = 0; const isValid = pattern.test(value);

// Or find every match in a block of text const matches = [...text.matchAll(pattern)]; `

A global regex carries lastIndex between calls, so reusing one across test() calls returns alternating results. Either drop the g flag for validation or reset it each time.

Anchors, Greediness and Backtracking

Three behaviours account for most regex surprises, and this pattern shows all three.

Anchors. ^ and $ pin the match to the start and end of the input. Without them, \d{3} matches the 123 inside abc123def. With m in the flags β€” as here β€” they pin to each *line* instead, which is what lets one pattern be tested against a list.

Greediness. .* takes as much as it can and gives back only when forced; .*? takes as little as possible. On , the pattern <.*> matches the whole string and <.*?> matches just .

Backtracking. When a match fails, the engine reverses and tries other splits. Nested quantifiers like (a+)+ make that exponential, and a 30-character input can hang a server β€” a class of denial of service known as ReDoS. Avoid nesting quantifiers, and prefer explicit character classes over . wherever you can.

Testing It Properly

`javascript const pattern = /^\+?1?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})$/gm;

// A global regex keeps lastIndex between calls, so reusing one across // test() calls returns alternating true/false on the same input. pattern.lastIndex = 0;

// Named groups make the result readable const named = /(?\d{4})-(?\d{2})/; const { groups } = '2026-08'.match(named); `

Write the failing cases first. A pattern that accepts everything valid is easy; one that also rejects everything invalid is the hard half, and it is where the bugs are.

When Not to Use a Regex

Structured formats have parsers, and the parser is always more correct: new URL() for URLs, DOMParser for HTML, JSON.parse` for JSON, a date library for dates. Reach for a regex to *find* things in unstructured text, not to validate something a parser understands.

Frequently Asked Questions

Does this work for international numbers?

This pattern is designed for US/Canada numbers (10 digits). For international numbers, use a more flexible pattern or a dedicated library like libphonenumber.

What regex flavor does this use?

This tester uses JavaScript regex (ECMAScript). Most patterns work the same in Python, Java, and other languages.

Related Tools

Explore other tools you might find useful:

More Regex Tester & Debugger tools

You might also need