2021年4月8日星期四

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));          
https://stackoverflow.com/questions/67014281/return-the-index-of-each-string-that-contain-a-phone-number-javascript April 09, 2021 at 10:30AM

没有评论:

发表评论