236

I want to write a regular expression for a standard US type phone number that supports the following formats:

###-###-####
(###) ###-####
### ### ####
###.###.####

where # means any number. So far I came up with the following expressions

^[1-9]\d{2}-\d{3}-\d{4}
^\(\d{3}\)\s\d{3}-\d{4}
^[1-9]\d{2}\s\d{3}\s\d{4}
^[1-9]\d{2}\.\d{3}\.\d{4}

respectively. I am not quite sure if the last one is correct for the dotted check. I also want to know if there is any way I could write a single expression instead of the 4 different ones that cater to the different formats I mentioned. If so, I am not sure how do I do that. And also how do I modify the expression/expressions so that I can also include a condition to support the area code as optional component. Something like

+1 ### ### ####

where +1 is the area code and it is optional.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
noobcoder
  • 11,983
  • 10
  • 39
  • 62
  • 6
    possible duplicate of http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation the suggested answer is to strip every non-digit character. In this way, you simplify the validation – Arnaud Denoyelle May 22 '13 at 18:24
  • 1
    I know this was a while back, but I don't think US area codes can begin with 1. (123) 456-7890 would be invalid because of the leading 1. – bobanahalf Jul 31 '15 at 19:49
  • For a more complete correct answer see: https://stackoverflow.com/a/18626090/561710 – Joe Johnston Sep 11 '17 at 17:23
  • Parsing phone numbers is hard. Google released an open source lib for this. Help yourself, use [libphonenumber](https://github.com/googlei18n/libphonenumber) (or a fork in your language) – aloisdg Mar 30 '18 at 15:05
  • If you are trying to do this, you are probably doing it wrong. Phone numbers are of varying lengths, include different country codes and in general are wierder than you think. Python and Java both have libraries that will parse phone numbers contextually and you should be using those kind of tools instead of trying to get a regex to do the job. – boatcoder May 08 '22 at 12:54

23 Answers23

376
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$

Matches the following

123-456-7890
(123) 456-7890
123 456 7890
123.456.7890
+91 (123) 456-7890

If you do not want a match on non-US numbers use

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

Update :
As noticed by user Simon Weaver below, if you are also interested in matching on unformatted numbers just make the separator character class optional as [\s.-]?

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

https://regex101.com/r/j48BZs/2

rolling_codes
  • 15,174
  • 22
  • 76
  • 112
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • 1
    Do you think that the question mark is not necessary in (?\d{3}\) part of your very first line ? I think we do need one or more occurrence and not zero or one occurrence of a digit within the '(' and ')' – noobcoder May 22 '13 at 19:54
  • 3
    The `?` there applies on parentheses `()`, not on the digits. The complete related regex is `\(?\d{3}\)?`. `\d{3}` specifies that there must be three digits between the `()` that are (made) optional (by `?`). – Ravi K Thapliyal May 22 '13 at 20:07
  • I may not have understood that completely but I think "?\\(" means we are checking for an opening parenthesis and right ahead ?\d{3} part checks for zero or one occurrence of a digit. So here I think the second question mark right before the digt is checking for digit and not the parenthesis. Or may be I am wrong – noobcoder May 22 '13 at 20:13
  • Ok. Now I get it. So in that case, should we also include a question mark at the very beginning like "^(?" to make the opening parenthesis of the area code to be optional as well ? – noobcoder May 22 '13 at 20:26
  • No, because the "(" in two cases are quite different. In the first case they are marking a region around the area code (i.e. they are not part of the text to be matched) and the ? after it then makes the whole region optional. In the second case since the parentheses is preceded by the escape character "\\(?" it does not mark the start of a region and instead starts matching on the "(" character itself. The ? after it then makes the presence of "(" in the text to be matched optional. – Ravi K Thapliyal May 22 '13 at 21:25
  • 9
    note: this doesn't match 1234567890 which may or may not be a problem. for me it was - so I just added `?` after each `[\s.-]` to make it optional – Simon_Weaver Aug 12 '14 at 20:59
  • 1
    @Simon_Weaver Thank you for your inputs. I've added your observation to the answer. – Ravi K Thapliyal Jan 12 '15 at 15:17
  • I'm not a pro at regex (which is why I came here) but I couldn't get this updated version to match "123-456-7890'. I'm only matching US telephones. I used this updated pattern from above: ^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$ And used Replace pattern to put it in US format: \1 \2-\3 Thanks for any suggestions. – StewS2 Mar 29 '16 at 16:18
  • fails on (224) - 612 - 1000 – Jakobovski Mar 09 '17 at 16:04
  • 1
    Does not handle the trivial case 1234567890 nor does it handle non-standard but valid us formats 123-4567890 123456-7890 or (123)4567890 or a number of other standards. – Bryant Makes Programs May 25 '17 at 13:07
  • 1
    Also passes these: (123 123 1234 and 123) 123 1234 – Bob Ray May 28 '18 at 20:48
  • 3
    If you want one that avoids the issue @BobRay mentioned, use `^(\+\d{1,2}\s)?((\(\d{3}\))|(\d{3}))[\s.-]\d{3}[\s.-]\d{4}$`. (I basically just duplicated the segment of the RegEx that covers the area code and allowed one variant with parens and one without) – Shrey Gupta Sep 17 '18 at 11:35
  • This will not match a phone number without spaces or special characters. For example: `2223334444`... – Radmation Apr 23 '19 at 17:24
  • @skybondsor lets see if there is any issue in future after testing.... – Prasad Shinde May 06 '20 at 11:14
  • ^(?\+?\d{1,2}|1)?[-. (]*(?[2-9](?!11)\d{2})[-. )]*(?\d{3})[-. ]*(?\d{4})(?: *x(\d+))?$ Here is a version where the sections are named, this helps if you need to extract the values and do something with them. – Joshua G Dec 15 '20 at 18:35
  • the space after country code ought to be optional: `^(\+\d{1,2}\s?)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$` – rolling_codes Feb 20 '23 at 02:12
  • to support +1-703-678-5982 format, we can add one more delimiter segment as follows `^(\+\d{1,2}\s?)?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$` – Ravi Misra Feb 26 '23 at 15:44
235

There are many variations possible for this problem. Here is a regular expression similar to an answer I previously placed on SO.

^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$

It would match the following examples and much more:

18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
800 555 1234x5678
8005551234 x5678
1    800    555-1234
1----800----555-1234

Regardless of the way the phone number is entered, the capture groups can be used to breakdown the phone number so you can process it in your code.

  • Group1: Country Code (ex: 1 or 86)
  • Group2: Area Code (ex: 800)
  • Group3: Exchange (ex: 555)
  • Group4: Subscriber Number (ex: 1234)
  • Group5: Extension (ex: 5678)

Here is a breakdown of the expression if you're interested:

^\s*                #Line start, match any whitespaces at the beginning if any.
(?:\+?(\d{1,3}))?   #GROUP 1: The country code. Optional.
[-. (]*             #Allow certain non numeric characters that may appear between the Country Code and the Area Code.
(\d{3})             #GROUP 2: The Area Code. Required.
[-. )]*             #Allow certain non numeric characters that may appear between the Area Code and the Exchange number.
(\d{3})             #GROUP 3: The Exchange number. Required.
[-. ]*              #Allow certain non numeric characters that may appear between the Exchange number and the Subscriber number.
(\d{4})             #Group 4: The Subscriber Number. Required.
(?: *x(\d+))?       #Group 5: The Extension number. Optional.
\s*$                #Match any ending whitespaces if any and the end of string.

To make the Area Code optional, just add a question mark after the (\d{3}) for the area code.

Francis Gagnon
  • 3,545
  • 1
  • 16
  • 25
44

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

Matches these phone numbers:

1-718-444-1122
718-444-1122
(718)-444-1122
17184441122
7184441122
718.444.1122
1718.444.1122
1-123-456-7890
1 123-456-7890
1 (123) 456-7890
1 123 456 7890
1.123.456.7890
+91 (123) 456-7890
18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
18001234567
1 800 123 4567
1-800-123-4567
+18001234567
+1 800 123 4567
+1 (800) 123 4567
1(800)1234567
+1800 1234567
1.8001234567
1.800.123.4567
+1 (800) 123-4567
18001234567
1 800 123 4567
+1 800 123-4567
+86 800 123 4567
1-800-123-4567
1 (800) 123-4567
(800)123-4567
(800) 123-4567
(800)1234567
800-123-4567
800.123.4567
1231231231
123-1231231
123123-1231
123-123 1231
123 123-1231
123-123-1231
(123)123-1231
(123)123 1231
(123) 123-1231
(123) 123 1231
+99 1234567890
+991234567890
(555) 444-6789
555-444-6789
555.444.6789
555 444 6789
18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1.800.555.1234
+1.800.555.1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
(003) 555-1212
(103) 555-1212
(911) 555-1212
18005551234
1 800 555 1234
+86 800-555-1234
1 (800) 555-1234

See regex101.com

stomy
  • 1,778
  • 1
  • 15
  • 16
  • With a slight modification, this also supports local phone numbers without the area code: `((\+\d{1,2}\s?)?1?\-?\.?\s?\(?\d{3}\)?[\s.-]?)?\d{3}[\s.-]?\d{4}` – TayTay Dec 09 '21 at 13:32
  • this is the best; I came back years later to find this – MoKi Apr 16 '23 at 16:37
23

Regex pattern to validate a regular 10 digit phone number plus optional international code (1 to 3 digits) and optional extension number (any number of digits):

/(\+\d{1,3}\s?)?((\(\d{3}\)\s?)|(\d{3})(\s|-?))(\d{3}(\s|-?))(\d{4})(\s?(([E|e]xt[:|.|]?)|x|X)(\s?\d+))?/g

Demo: https://www.regextester.com/103299

Valid entries:

/* Full number */
+999 (999) 999-9999 Ext. 99999

/* Regular local phone number (XXX) XXX-XXXX */
1231231231
123-1231231
123123-1231
123-123 1231
123 123-1231
123-123-1231
(123)123-1231
(123)123 1231
(123) 123-1231
(123) 123 1231

/* International codes +XXX (XXX) XXX-XXXX */
+99 1234567890
+991234567890

/* Extensions (XXX) XXX-XXXX Ext. XXX... */
1234567890 Ext 1123123
1234567890Ext 1123123
1234567890 Ext1123123
1234567890Ext1123123

1234567890 Ext: 1123123
1234567890Ext: 1123123
1234567890 Ext:1123123
1234567890Ext:1123123

1234567890 Ext. 1123123
1234567890Ext. 1123123
1234567890 Ext.1123123
1234567890Ext.1123123

1234567890 ext 1123123
1234567890ext 1123123
1234567890 ext1123123
1234567890ext1123123

1234567890 ext: 1123123
1234567890ext: 1123123
1234567890 ext:1123123
1234567890ext:1123123

1234567890 X 1123123
1234567890X1123123
1234567890X 1123123
1234567890 X1123123
1234567890 x 1123123
1234567890x1123123
1234567890 x1123123
1234567890x 1123123
smonff
  • 3,399
  • 3
  • 36
  • 46
bcngr
  • 745
  • 6
  • 13
10

Here's a fairly compact one I created.

Search: \+?1?\s*\(?-*\.*(\d{3})\)?\.*-*\s*(\d{3})\.*-*\s*(\d{4})$

Replace: +1 \($1\) $2-$3

Tested against the following use cases.

18001234567
1 800 123 4567
1-800-123-4567
+18001234567
+1 800 123 4567
+1 (800) 123 4567
1(800)1234567
+1800 1234567
1.8001234567
1.800.123.4567
1--800--123--4567
+1 (800) 123-4567
Puneet Lamba
  • 755
  • 8
  • 15
6

Adding up an example using above mentioned solutions on jsfiddle. I have modified the code a bit as per my clients requirement. Hope this also helps someone.

/^\s*(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/

See Example Here

Avinash Lad
  • 61
  • 1
  • 1
6

Phone number regex that I use: /^[+]?(\d{1,2})?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/

Covers:

  • 18001234567
  • 1 800 123 4567
  • +1 800 123-4567
  • +86 800 123 4567
  • 1-800-123-4567
  • 1 (800) 123-4567
  • (800)123-4567
  • (800) 123-4567
  • (800)1234567
  • 800-123-4567
  • 800.123.4567
5

try this for Pakistani users .Here's a fairly compact one I created.

((\+92)|0)[.\- ]?[0-9][.\- ]?[0-9][.\- ]?[0-9]

Tested against the following use cases.

+92 -345 -123 -4567
+92 333 123 4567
+92 300 123 4567
+92 321 123 -4567
+92 345 - 540 - 5883
sajid
  • 49
  • 1
  • 6
4

Starting with @Ravi's answer, I also applied some validation rules for the NPA (Area) Code.

In particular:

  • It should start with a 2 (or higher)
  • It cannot have "11" as the second and third digits (N11).

There are a couple other restrictions, including reserved blocks (N9X, 37X, 96X) and 555, but I left those out, particularly because the reserved blocks may see future use, and 555 is useful for testing.

This is what I came up with:

^((\+\d{1,2}|1)[\s.-]?)?\(?[2-9](?!11)\d{2}\)?[\s.-]?\d{3}[\s.-]?\d{4}$

Alternately, if you also want to match blank values (if the field isn't required), you can use:

(^((\+\d{1,2}|1)[\s.-]?)?\(?[2-9](?!11)\d{2}\)?[\s.-]?\d{3}[\s.-]?\d{4}$|^$)

My test cases for valid numbers (many from @Francis' answer) are:

18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1.800.555.1234
+1.800.555.1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234

My invalid test cases include:

(003) 555-1212     // Area code starts with 0
(103) 555-1212     // Area code starts with 1
(911) 555-1212     // Area code ends with 11
180055512345       // Too many digits
1 800 5555 1234    // Prefix code too long
+1 800 555x1234    // Invalid delimiter
+867 800 555 1234  // Country code too long
1-800-555-1234p    // Invalid character
1 (800)  555-1234  // Too many spaces
800x555x1234       // Invalid delimiter
86 800 555 1212    // Non-NA country code doesn't have +

My regular expression does not include grouping to extract the digit groups, but it can be modified to include those.

MCattle
  • 2,897
  • 2
  • 38
  • 54
4

I find this regular expression most useful for me for 10 digit contact number :

^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$

Reference: https://regex101.com/r/QeQewP/1

Explanation:

enter image description here

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
Amitesh Bharti
  • 14,264
  • 6
  • 62
  • 62
  • Did you use a tool to generate that explanation? Do share! Thanks – unnknown Jan 03 '19 at 20:38
  • 2
    @unnknown Looks like a screenshot from an online regex checker, such as regex101.com. Note that SO guidelines state that all text, code, data, and error messages must be input in text form, not images, because text in images can be difficult to read, particularly on mobile devices, and incurs greater bandwidth. Also text cannot be copy-pasted. In this case, the colors might add something, not sure. Know that generally, "Answers" should include a textual explanation. Amitesh: it would be useful to include a link to this site, with your regex already populated for visitors to experiment with. – SherylHohman Apr 13 '20 at 06:36
  • @unnknown here is a link that produces an image similar to the one in this post. Amitesh's regex string has been already pasted in. You can enter phone numbers to test its results: https://regex101.com/r/QeQewP/1 – SherylHohman Apr 13 '20 at 06:40
  • how to use this? – Rohan Devaki Feb 07 '22 at 10:38
  • @RohanDevaki Regex is a powerful tool used to validate, manipulate, and extract data from text. Different languages have different inbuilt methods to use it. Which language do you want to use? meanwhile, the following article provides an example for using regular expressions in 10 different languages. I would recommend to give it read https://blog.teamtreehouse.com/regular-expressions-10-languages – Amitesh Bharti Feb 09 '22 at 06:33
3

Perhaps the easiest one compare to several others.

\(?\d+\)?[-.\s]?\d+[-.\s]?\d+

It matches the following:

(555) 444-6789

555-444-6789

555.444.6789

555 444 6789

Community
  • 1
  • 1
MITHU
  • 113
  • 3
  • 12
  • 41
2

The expressions for 1, 3 and 4 are quite similar, so you can use:

^([1-9]\d{2})([- .])(\d{3})$2(\d{4})$

Note that, depending on the language and brand of regexes used, you might need to put \2 instead of $2 or such matching might not be supported at all.

I see no good way to combine this with the format 2, apart from the obvious ^(regex for 1,3,4|regex for 2)$ which is ugly, clumsy and makes it hard to get out the parts of the numbers.

As for the area code, you can add (\+\d)? to the beginning to capture a single-digit area code (sorry, I don't know the format of your area codes).

Vedran Šego
  • 3,553
  • 3
  • 27
  • 40
2

This code will match a US or Canadian phone number, and will also make sure that it is a valid area code and exchange:

^((\+1)?[\s-]?)?\(?[2-9]\d\d\)?[\s-]?[2-9]\d\d[\s-]?\d\d\d\d

Test on Regex101.com

Shino
  • 21
  • 2
2

This is my Regex the worked on US numbers in the FreeCodeCamp phone number challenge:

/^\d{3}(-|\s)\d{3}(-|\s)\d{4}$|^\d{10}$|^1\s\d{3}(-|\s)\d{3}(-|\s)\d{4}$|^(1\s?)?\(\d{3}\)(\s|\-)?\d{3}\-\d{4}$/

Matches:

555-555-5555

(555)555-5555

(555) 555-5555

555 555 5555

5555555555

1 555 555 5555 etc
Harvey Kadyanji
  • 515
  • 6
  • 8
1

How about this?

^(\+?[01])?[-.\s]?\(?[1-9]\d{2}\)?[-.\s]?\d{3}[-.\s]?\d{4}

EDIT: I forgot about the () one. EDIT 2: Got the first 3 digit part wrong.

crimson_penguin
  • 2,728
  • 1
  • 17
  • 24
  • There is no need to escape dot `.` when in a character class, so `[-\.\s]` should actually be `[-.\s]`, because we don't want to match a backslash. – Vedran Šego May 22 '13 at 18:37
  • Would it actually match the backslash? I thought it might not be necessary, but I wasn't 100% sure. – crimson_penguin May 22 '13 at 18:46
  • You are right. I have just tried it (I never needed a backslash before) and `[\.]` and `[.]` both match only dot, while `[\\.]` matches both dot and backslash. Thank you for your remark. – Vedran Šego May 22 '13 at 18:55
  • 1
    My opinion about regexes is that, if you are giving a regex to someone, always accompany it with an explanation, or the regex is worthless. Just my opinion. – John Red Feb 10 '17 at 05:42
1

Above regex is a slight modification of @Francis Gagnon.

Objective : To detect any possible pattern a user can share their US phone number


Version 1:

^\s*(?:\+?(\d{1,3}))?[\W\D\s]*(\d[\W\D\s]*?\d[\D\W\s]*?\d)[\W\D\s]*(\d[\W\D\s]*?\d[\D\W\s]*?\d)[\W\D\s]*(\d[\W\D\s]*?\d[\D\W\s]*?\d[\W\D\s]*?\d)(?: *x(\d+))?\s*$

Test it over here Codepen: https://codepen.io/kiranbhattarai/pen/NWKMXQO

Explanation of the regex : https://regexr.com/4kt5j


Version 2:

\s*(?:\+?(\d{1,3}))?[\W\D\s]^|()*(\d[\W\D\s]*?\d[\D\W\s]*?\d)[\W\D\s]*(\d[\W\D\s]*?\d[\D\W\s]*?\d)[\W\D\s]*(\d[\W\D\s]*?\d[\D\W\s]*?\d[\W\D\s]*?\d)(?: *x(\d+))?\s*$

What is in it: The test cases can be a part of the string. In version one the test cases should be a start of a line to work.

Codepen: https://codepen.io/kiranbhattarai/pen/GRKGNGG

Explanation of the regex : https://regexr.com/4kt9n


If you can find a pattern that can fail please do comment i will fix it.

Test Cases: Pass

8 0   0  4  4  4  5   55 5
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
800 555 1234x5678
8005551234 x5678
1    800    555-1234
1----800----555-1234
800 (555) 1234
800(555)1234
8 0 0 5 5 5 1 2 3 4
8.0.0.5.5.5.1.2.3.4
8-0-0-5-5-5-1-2-3-4
(8)005551234
(80)05551234
8(00)5551234
8@0@0@5551234
8/0/0/5/5/5/1/2/3/4
8*0*0*5*5*5*1*2*3*4
8:0:0:5:5:5:1:2:3:4
8,0,0,5,5,5,1,2,3,4
800,555,1234
800:555:1234
1-718-444-1122
718-444-1122
(718)-444-1122
17184441122
7184441122
718.444.1122
1718.444.1122
1-123-456-7890
1 123-456-7890
1 (123) 456-7890
1 123 456 7890
1.123.456.7890
+91 (123) 456-7890
18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
18001234567
1 800 123 4567
1-800-123-4567
+18001234567
+1 800 123 4567
+1 (800) 123 4567
1(800)1234567
+1800 1234567
1.8001234567
1.800.123.4567
+1 (800) 123-4567
18001234567
1 800 123 4567
+1 800 123-4567
+86 800 123 4567
1-800-123-4567
1 (800) 123-4567
(800)123-4567
(800) 123-4567
(800)1234567
800-123-4567
800.123.4567
1231231231
123-1231231
123123-1231
123-123 1231
123 123-1231
123-123-1231
(123)123-1231
(123) 123-1231
(123) 123 1231
+99 1234567890
+991234567890
(555) 444-6789
555-444-6789
555.444.6789
555 444 6789
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1.800.555.1234
+1.800.555.1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
(003) 555-1212
(103) 555-1212
(911) 555-1212
18005551234
1 800 555 1234
+86 800-555-1234
1 (800) 555-1234
Kiran Bhattarai
  • 175
  • 1
  • 12
  • +86, +99 etc are definitely not US phone numbers. Several of your other test cases look dubious for other reasons. – tripleee Sep 12 '19 at 17:28
  • @tripleee let me fix that, i have updated it to any country i hope it works. Additionally i removed the duplicate test cases as well – Kiran Bhattarai Sep 12 '19 at 19:42
1

I'm just throwing this answer in there since it solves a problem of mine, it's based off of @stormy's answer, but includes 3 digit country codes and more importantly can be used anywhere in a string, but won't match is it's not preceded by a space/start of the string and ending with a word boundary. This is useful so that it won't match random numbers in the middle of a URL or something

((?:\s|^)(?:\+\d{1,3}\s?)?1?\-?\.?\s?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4})(?:\b)
hobberwickey
  • 6,118
  • 4
  • 28
  • 29
1

Here's a regex that matches North American numbers as well as international numbers such as for middle east.

^((\+|0{0,2})([0-9]){1,3})?[-.●\s]?\(?([0-9]{2,3})\)?[-.●\s]?([0-9]{3})[-.●\s]?([0-9]{4})$

smartexpert
  • 2,625
  • 3
  • 24
  • 41
1

I know this doesn't answer OP's question directly but if you are asking the same question as OP there is a good chance your are looking for a way to validate and store a phone number in either state or a database. Instead of trying to detect every possible combination of character that could be a phone number you might find it easier to break this task into multiple steps.

  1. strip out all none numbers
  2. strip out leading 1s
  3. make sure the number is at most 10 digits

Javascript pseudo example assuming "phone" is user input stored as a string:

phone.replace(/\D/g, "") 
phone.replace(/^1+/g, "") 
phone.slice(0, 10) 
phone.length === 10 ? "do something" : "don't do something"
  • Code above will need to be tweaked for your purposes and is left as simple as possible for none javascript readers.

For presentation purposes you can always layer dashes and leading 1s back in later but for storage you should probable only keep the actual numbers. This approach also has the added advantage of leaving you with some easy to digest regular expressions.

Glenn S
  • 31
  • 4
0
^(\+1)?\s?(\([1-9]\d{2}\)|[1-9]\d{2})(-|\s|.)\d{3}(-|\s|.)\d{4}
amalgamate
  • 2,200
  • 5
  • 22
  • 44
0

This is a more comprehensive version that will match as much as I can think of as well as give you group matching for country, region, first, and last.

(?<number>(\+?(?<country>(\d{1,3}))(\s|-|\.)?)?(\(?(?<region>(\d{3}))\)?(\s|-|\.)?)((?<first>(\d{3}))(\s|-|\.)?)((?<last>(\d{4}))))
jake
  • 1
  • 1
0

what about multiple numbers with "+" and seperate them with ";" "," "-" or " " characters?

radeveloper
  • 926
  • 2
  • 12
  • 20
0

I ended up with

const regexBase = '(?:\\+?(\\d{1,3}))?[-. (]*(\\d{3})?[-. )]*(\\d{3})[-. ]*(\\d{4,5})(?: *x(\\d+))?'; const phoneRegex = new RegExp('\\s*' + regexBase + '\\s*', 'g');

this was to allow for things like dutch numbers, for example

+358 300 20200

Kelly Milligan
  • 578
  • 1
  • 4
  • 17