Regex for US Phone Numbers

by Captain Database on Sep 15th in Regex

I know that this is something people often say you can’t use regex for, but they’re wrong. If you want one magical regex that works for every possible country, extension, and format, then maybe that’s unreasonable. But with the right levels of internationalization, it’s certainly possible.

So here is what I could come up with, assuming that it should only work with US phone numbers:

1?[-(). ]{0,2}(\d{3})[-(). ]{0,2}(\d{3})[-(). ]{0,2}(\d{4})

I think this is a pretty good level of accuracy. It’ll support many formats, and the capture groups will also break up the area code, prefix, and line number for you. Now all I need to do is learn every other phone number format in the world…

Leave a Reply

Powered By Wordpress Designed By Ridgey