Regular Expressions 101

Community Patterns

North America phone number

1

Regular Expression
PCRE (PHP <7.3)

/
^(?'countryCode'\+?1)?[-.\s]?\(?(?'areaCode'\d{3,3})[)-.\s]{0,2}(?'phone1'\d{3,3})[-.\s]?(?'phone2'\d{4,4})\s*(?'extension'(?:x|ex|ext|extension|\s)\s*\d+)?$
/
g

Description

Relatively flexible pattern, intended to accept and parse North American phone numbers. Country code can be changed to accept any country phone number as long as it's of the general form ### ### #### (i.e. 10 digits, seperated into 3 digit area, 3 digit fisrt group, and 4 digit second group) This regex is ignorant of spaces. Also supports extensions.

Submitted by Roman P - 8 years ago