gpt4 book ai didi

Javascript 正则表达式替换电话号码格式

转载 作者:行者123 更新时间:2023-11-30 16:02:53 25 4
gpt4 key购买 nike

是否有一种正则表达式可用于生成如下所示的电话号码格式。或者我是否需要更多表达式/编码来处理以下所有内容。

2159018060    -> 215-901-8060
(215) 901 8060-> 215-901-8060
(215)-901-8060-> 215-901-8060
1.215.901.8060 -> 215-901-8060
+1-215-9018060 -> 215-901-8060
+1-215-901-8060 x233 -> 215-901-8060 x233
+1-215-901-8060 Ext 233 -> 215-901-8060 Ext 233
+44 20 7323 8299 -> +44 20 7323 8299

我有下面的一个可以处理大部分。

/^[\+\(1]?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/

但不确定如何处理 +1 开头

谢谢谢谢

最佳答案

这首先清除所有非数字字符。这(理论上)应该包括空格,所以如果你的有空格,你可能需要稍微改变一下。

正则表达式的下一位在字符串中搜索具有 3 组长度为 3、3 和 4 的匹配项。

注意要使其正常工作,请输入电话号码,然后按 Tab 键关闭。

包含 jQuery,让生活变得更快。

$(document).ready(function(){
$('#phone').change(function(){
var value = $(this).val();

value = value.replace(/[^\d]/g,'');
value = value.replace(/^([0-9]{3})([0-9]{3})([0-9]{4})$/,'($1) $2-$3');
$('#results').html(value);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="phone"/>
<div id="results"></div>

编辑

花了我一点时间,但这应该可以解决您的问题。如果有任何问题,请告诉我。

var nums_to_try = [
'2159018060',
'(215) 901 8060',
'(215)-901-8060',
'1.215.901.8060',
'+1-215-9018060',
'+1-215-901-8060 x233',
'+1-215-901-8060 Ext 233',
'+44 20 7323 8299'
]; //quick test to make sure all demonstrated cases are input

$(document).ready(function() {

var numbers = [{
name: 'phone10', //property was mostly used for debug. you can probably remove this without any errors
pattern: /^([0-9]{3})\-?([0-9]{3})\-?([0-9]{4})(ext|x)?([0-9]+)?$/i,
//starts with 3 numbers, another 3 numbers, then 4 numbers. we either have ext or x, and then some more numbers (though we don't know how many)
replace: '$1-$2-$3 $4 $5' //If $4 and $5 aren't there we remove the extra space with the .trim() call at the end.
}, {
name: 'phone12',
pattern: /(\+[0-9]{2})([0-9]{2})([0-9]{4})([0-9]{4})/,
//a group of two with a plus, another group of two, and then 2 groups of 4.
replace: '$1 $2 $3 $4'
}]; //Array of number maps

var not_allowed = [
/\./g, //no dots.
/^\+?1-?/g, //removes leading 1's with option +1 or 1- or +1- syntax
/[\(\)\-\s]/g //gets rid of both parenthesis characters, hyphens, and spaces.
]; //Array of not allowed characters
var remove_not_allowed = function(str, n) {
//str we are using,
//patterns or strings we are not allowing. We simply erase them.
$.each(n, function(i, ni) {
str = str.replace(ni, '');
}); //Loops through each object in n, replacing it.
return str;
};

var mapped_nums = $.map(nums_to_try, function(phone) {
var phone = remove_not_allowed(phone, not_allowed);
//gets rid of all our non-allowed characters or matches
$.each(numbers, function(i, rep) {
//loops through each phone number array
if (phone.match(rep.pattern)) { //Check that the pattern applies, otherwise nothing is going to work.
phone = phone.replace(rep.pattern, rep.replace);
//if it matches, replace.
return; //if it matches, we don't want duplicate matches so we end.
}

});
return phone.trim(); //returns our trimmed out phone number
}); //end $.map call
console.log(mapped_nums); //console check just to make sure we aren't adding any spaces to the end of our strings.
$('#results').html(mapped_nums.join('\n')); //Quick way to test our results.
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<pre id="results">
2159018060 -> 215-901-8060
(215) 901 8060-> 215-901-8060
(215)-901-8060-> 215-901-8060
1.215.901.8060 -> 215-901-8060
+1-215-9018060 -> 215-901-8060
+1-215-901-8060 x233 -> 215-901-8060 x233
+1-215-901-8060 Ext 233 -> 215-901-8060 Ext 233
+44 20 7323 8299 -> +44 20 7323 8299
</pre>

关于Javascript 正则表达式替换电话号码格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37447571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com