我有联系人列表,需要删除国家代码(+91),数字之间的空格和零(前缀为零)从手机号码。并且它应该只包含10位数字。
我试着用下面的方法使用正则表达式,但它只删除了数字中的空格。
var value = "+91 99 16 489165";
var mobile = '';
if (value.slice(0,1) == '+' || value.slice(0,1) == '0') {
mobile = value.replace(/[^a-zA-Z0-9+]/g, "");
} else {
mobile = value.replace(/[^a-zA-Z0-9]/g, "");
}
console.log(mobile);发布于 2018-10-29 18:10:04
var value = "+91 99 16 489165";
var number = value.replace(/\D/g, '').slice(-10);发布于 2018-10-29 18:18:36
如果你确定"+“或"0”后面有国家代码,你可以使用string.substr。
var value="+91 99 16 489165";
var mobile = '';
if(value.charAt(0) == '+' || value.charAt(0)=='0'){
mobile = value.replace(/[^a-zA-Z0-9+]/g, "").substr(3);
}
else {
mobile = value.replace(/[^a-zA-Z0-9]/g, "");
}发布于 2018-10-29 18:18:13
var value="+91 99 16 489165";
// Remove all spaces
var mobile = value.replace(/ /g,'');
// If string starts with +, drop first 3 characters
if(value.slice(0,1)=='+'){
mobile = mobile.substring(3)
}
// If string starts with 0, drop first 4 characters
if(value.slice(0,1)=='0'){
mobile = mobile.substring(4)
}
console.log(mobile);https://stackoverflow.com/questions/53043128
复制相似问题