Skip to content Skip to sidebar Skip to footer

Regex To Fetch Phone Number From String

I want regex which finds out continues max 12 digits long number by ignoring space, plus (+), parenthesis & dash, e.g: Primary contact number +91 98333332343 call me on this My

Solution 1:

I see that there are numbers ranging from 10 to 13 digits.

You may use

/(?:[-+() ]*\d){10,13}/g

See the regex demo.

Details:

  • (?:[-+() ]*\d){10,13} - match 10 to 13 sequences of:
    • [-+() ]* - zero or more characters that are either -, +, (, ), or a space
    • \d - a digit

var re = /(?:[-+() ]*\d){10,13}/gm; 
var str = 'Primary contact number +91 98333332343 call me on this\nMy number is +91-983 333 32343\n2nd number +1 (983) 333 32343, call me\nAnother one 983-333-32343\nOne more +91(983)-333-32343 that\'s all\n121 street pin code 421 728 & number is 9833636363';
var res = str.match(re).map(function(s){return s.trim();});
console.log(res);
 

Solution 2:

The accepted answer will match your criteria but I'd like to propose a more restrictive approach. It is quite specific to the number formats you provided :

  • test specifically if a string IS a number /^(\+(\d{1,2})[- ]?)?(\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4,5}$/
  • test whether a string contains at least one number : /(\+(\d{1,2})[- ]?)?(\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4,5}/

I made you a small fiddle where you can try out different regexes on any number of... well numbers : https://jsfiddle.net/u51xrcox/5/.

have fun.


Post a Comment for "Regex To Fetch Phone Number From String"