JavaScript / ECMAScript

US Phone Number in JS

Match US phone numbers in common formats: (555) 867-5309, 555-867-5309, 5558675309.

Try it in the JS tester →

Pattern

regexJS
\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("\\(?\\d{3}\\)?[\\s.\\-]?\\d{3}[\\s.\\-]?\\d{4}", "g");
const input = "(555) 867-5309";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));

Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).

How the pattern works

The optional \(? and \)? allow parentheses around the area code. The [\s.\-]? between groups allows spaces, dots, or hyphens as optional separators.

Examples

Input

(555) 867-5309

Matches

  • (555) 867-5309

Input

555.867.5309

Matches

  • 555.867.5309

Input

5558675309

Matches

  • 5558675309

Same pattern, other engines

← Back to US Phone Number overview (all engines)