Validate US Telephone Numbers


正确结果:
1 555-555-5555
2 1 555-555-5555
3 1 (555) 555-5555
4 555-555-5555
5 (555)555-5555
6 1(555)555-5555
7 1 555 555 5555
8 1 456 789 4444
9 5555555555

本题主要考验的是正则表达式的运用;

JS正则表达式大全

在线正则表达式测试

代码:
 1 function telephoneCheck(str) {
 2   // 祝你好运
 3   if(str.match(/^1?\s?(\d{3}|\(\d{3}\))[-\s]?\d{3}[-\s]?\d{4}$/)){
 4     return true;
 5   }else{
 6     return false;
 7   }
 8   
 9 }
10 
11 
12 
13 telephoneCheck("555-555-5555");

 

 posted on 2017-08-08 18:57  lailailee  阅读(380)  评论(0编辑  收藏  举报