英文:
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("my numberis 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)
}
}
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 = ['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)
})
<!-- 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 = "my numberis 0987654321";
// filter symbols which can separate phone number dights
input = input.split(" ").join("").split("-").join("");
let phoneNumber = input.match(/\d{10,}/)[0];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论