Return the index of each string that contain a Phone Number? (Javascript)

I am trying to return the index of each string that contain a Phone Number.

var array = [
  'Just a Call Away, call 314-867-5309 ',
  'The Current city population is 3,443,940,573.',
  'My number is (123) 456-7890',
];

Expected Output: [0,2]

This is what I got so far, I used phoneRe as my regex. I can't seem to figure out how to filter out the string with 3,443,940,573 in my solution below.


function phoneNumber(p){
  var numArray = [];
  var phoneRe = /^[1-9]\d{2}[1-9]\d{2}\d{4}$/;
      
  for (var i = 0; i <p.length; i++){
    
    if (phoneRe.test(p[i].replace(/\D/g, ""))===true){
      numArray.push(i);
    }
}
  return numArray;
}

console.log(phoneNumber(array));
 

 


Read more here: https://stackoverflow.com/questions/67014281/return-the-index-of-each-string-that-contain-a-phone-number-javascript

Content Attribution

This content was originally published by StaceyW at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: