0
votes

I have a regex that trying to match these phone number format. I'm super close except 1 scenario:

Regex: ^\(?(\d{3})\)?[-\. ]?(\d{3})[-\. ]?(\d{4})$

Phone number format are trying to match:

  • (123) 456-7890
  • (123) 4567890
  • 123-456-7890
  • 123-4567890
  • 1234567890
  • 1234567890

I also want to match this.... there is leading spaces before and after the area code:

[empty spaces] (123) [emtpy spaces] 456-7890

1

1 Answers

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

This should do it.

The * character means 0 or more of preceding.

Regexr is probably the best tool I know to figure out how to change regex patterns.