在JavaScript中从字符串提取一个包含10位数字的手机号码的正则表达式:

huangapple go评论88阅读模式
英文:

Regular expression to extract a 10 digit mobile number from string in javascript

问题

我希望验证我的输入参数是否是一个包含10位数字的电话号码,即使输入中包含其他文本。

我尝试过以下代码:

numbemail("my number is 0987654321")
function numbemail(data){
    var input = data;  
    var numb = new RegExp(
       "\\+?\\(\\d*\\)? ?\\(\\d+\\)?\\d*([\\s\\-.]?\\d{2,})+", "g"
    );
    var numbresult = input.match(numb);
    if(numbresult){
       var re = []
       if(numbresult[0].length === 10){
          re.push(numbresult[0])
       } else if(numbresult[0].length < 10) {  
          re.push(numbresult[0] + " ==> (WrongNumber: you provided Number is less than 10 digits)</p>")
       } else if(numbresult[0].length > 10) { 
          re.push(numbresult[0] + " ==> (WrongNumber: you provided Number is more than 10 digits)</p>")
       }
       console.log(re)
    } 
}

当我尝试这个代码时,期望得到10位数字,但我得到了numbresult[0].length=11

如果我使用一个包含10位数字的字符串,那么numbresult[0].length=10

期望输入:

input = ("my mobile number is 9087654321")

期望输出:

"9087654321"
英文:

I wish to validate if my input argument is a 10 digit phone number, even if there is additional text in the input.

I have tried this:

numbemail(&quot;my numberis 0987654321&quot;) 
function numbemail(data){
var input =data;  
var numb = new RegExp(
   &quot;\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s\\-.]?\\d{2,})+&quot;, &quot;g&quot;
);
var numbresult = input.match(numb);
if(numbresult){
   var re=[]
   if(numbresult[0].length === 10){
      re.push(numbresult[0])
   } else if(numbresult[0].length &lt; 10) {  
      re.push(numbresult[0]+&quot; ==&gt;(WrongNumber: you provided Number is less than 10 digits)&lt;/p&gt;&quot;)
   } else if(numbresult[0].length &gt; 10) { 
      re.push(numbresult[0]+&quot; ==&gt; (WrongNumber: you provided Number is more than 10 digits)&lt;/p&gt;&quot;)
   }
   console.log(re)
   } 
}

When I tried this expecting 10 digits, but I got numbresult[0].length= 11

If I used a 10 digit string, then numbresult[0].length= 10

Expected Input:
>input = ("my mobile number is 9087654321")

Expected Output:
>"9087654321"

答案1

得分: 1

尝试这个:

var regex = /[\+]?\d{10}/

let strings = ['my numberis 0987654321', 'my number 8 is 9087654321', 'my number 8 is 9087654321123', 'my number 8 is 9087654']

strings.forEach(str => {
 let output = str.match(regex)
 if(output){
   console.log('number is valid', output[0])
   return
 }
    console.log('number is not valid - kindly enter 10 digit mobile number', output)
})

注意:此代码是JavaScript代码示例,用于匹配字符串中的10位数字。

英文:

Try this one

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var regex = /[\+]?\d{10}/

let strings = [&#39;my numberis 0987654321&#39;, &#39;my number 8 is 9087654321&#39;, &#39;my number 8 is 9087654321123&#39;, &#39;my number 8 is 9087654&#39;]


strings.forEach(str =&gt; {
 let output = str.match(regex)
 if(output){
   console.log(&#39;number is valid&#39;, output[0])
   return
 }
    console.log(&#39;number is not valid - kindly enter 10 digit mobile number&#39;, output)
})

<!-- end snippet -->

答案2

得分: 0

尝试简化你的正则表达式。你为简单的任务写了太复杂的代码。

let input = "my numberis 0987654321";
// 过滤可以分隔电话号码数字的符号
input = input.split(" ").join("").split("-").join("");
let phoneNumber = input.match(/\d{10,}/)[0];
英文:

Try to make your RegExp more simple. You write too complex code for simple task.

let input = &quot;my numberis 0987654321&quot;;
// filter symbols which can separate phone number dights
input = input.split(&quot; &quot;).join(&quot;&quot;).split(&quot;-&quot;).join(&quot;&quot;);
let phoneNumber = input.match(/\d{10,}/)[0];

huangapple
  • 本文由 发表于 2020年1月6日 20:31:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定