我有联系人列表,需要从手机号码中删除国家/地区代码(+91),数字和零之间的空格(带零的前缀)。并且只能包含10位数字。
我曾尝试以以下方式使用Regex,但它仅从数字中删除空格。
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);
答案 0 :(得分:1)
data = [{'Key': '1',
'Title': 'Salvation.S02E11.HDTV.x264-KILLERS',
'Seeds': '262',
'Leechers': '19'},
{'Key': '2',
'Title': 'Salvation.S02E13.WEB.x264-TBS[ettv]',
'Seeds': '229',
'Leechers': '25'},
{'Key': '3',
'Title': 'Salvation.S02E08.HDTV.x264-KILLERS',
'Seeds': '178',
'Leechers': '21'},
{'Key': '4',
'Title': 'Salvation.S02E01.HDTV.x264-KILLERS',
'Seeds': '144',
'Leechers': '11'},
{'Key': '5',
'Title': 'Salvation.S02E09.HDTV.x264-SVA[ettv]',
'Seeds': '129',
'Leechers': '14'}]
keys = ['Key', 'Title', 'Seeds', 'Leechers']
print(*to_aligned_data(data, keys=keys),
sep='\n')
# 1 Salvation.S02E11.HDTV.x264-KILLERS 262 19
# 2 Salvation.S02E13.WEB.x264-TBS[ettv] 229 25
# 3 Salvation.S02E08.HDTV.x264-KILLERS 178 21
# 4 Salvation.S02E01.HDTV.x264-KILLERS 144 11
# 5 Salvation.S02E09.HDTV.x264-SVA[ettv] 129 14
keys = ['Title', 'Leechers']
print(*to_aligned_data(data, keys=keys),
sep='\n')
# Salvation.S02E11.HDTV.x264-KILLERS 19
# Salvation.S02E13.WEB.x264-TBS[ettv] 25
# Salvation.S02E08.HDTV.x264-KILLERS 21
# Salvation.S02E01.HDTV.x264-KILLERS 11
# Salvation.S02E09.HDTV.x264-SVA[ettv] 14
keys = ['Key', 'Title', 'Seeds', 'Leechers']
print(*to_aligned_data(data, keys=keys, sep=' ' * 5),
sep='\n')
# 1 Salvation.S02E11.HDTV.x264-KILLERS 262 19
# 2 Salvation.S02E13.WEB.x264-TBS[ettv] 229 25
# 3 Salvation.S02E08.HDTV.x264-KILLERS 178 21
# 4 Salvation.S02E01.HDTV.x264-KILLERS 144 11
# 5 Salvation.S02E09.HDTV.x264-SVA[ettv] 129 14
答案 1 :(得分:1)
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);
答案 2 :(得分:1)
如果您确定在“ +”或“ 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, "");
}
答案 3 :(得分:1)
我希望这会有所帮助:
var value = "+91 99 16 489165";
var mobile = "";
//First remove all spaces:
value = value.replace(/\s/g, '');
// If there is a countrycode, this IF will remove it..
if(value.startsWith("+")){
var temp = value.substring(3, value.length);
mobile = "0"+temp;
//Mobile number:
console.log(mobile);
}
// If there is no countrycode, only remove spaces
else{
mobile = value;
//Mobile number:
console.log(mobile);
}
答案 4 :(得分:0)
这里是一个正则表达式,我只用来删除电话号码的国家/地区代码部分:
var mobile = value.replace(/^\+[0-9]{1,3}(\s|\-)/, "");
美国:
+1 345 345 7678
+ 1-453-677-7655
印度:
+91 99 16 489165
如果您的数据中以“ +”开头的国家/地区代码,则可以使用此网站上列出的所有国家/地区代码: https://countrycode.org/