Javascript extract phone number from string/text | RegEx read a mobile number

Using the regular expression and match method you can extract phone numbers from strings in JavaScript.

Le’t take three different patterns of number that you want to search.

  • 10 digits
  • + sign followed by 10 digits
  • (xxx)-xxxxxxX format

These patterns are matched with the following regex.

Example how to extract phone number from string in JavaScript

HTML Example code extract phone numbers from text in JS.

Output:

Javascript extract phone number from string

The metacharacter \d search for digits, which are also numbers. The match() method uses regular expressions to retrieve it results. When used the match() with \d, it returns the number.

Other solution: If you already know your string is a phone number, just replace all non-numeric characters to leave your phone number:

That way, it doesn’t matter what the format is, you always get the number.

If you don’t know your string has a phone number, and you’re just searching in the wild, you can use the RegEx’s in the other answers to read numbers from a string in JavaScript.

Do comment if you have any doubts and suggestions on this topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.