gpt4 book ai didi

Javascript正则表达式数值范围

转载 作者:行者123 更新时间:2023-11-28 20:25:46 26 4
gpt4 key购买 nike

有人可以帮助解决正则表达式吗?我正在尝试格式化电话号码并处理一系列分机号。我尝试使用范围 [1-5],但这似乎不起作用。

$(".phone").text(function(i, text) {
if(text.length == 10) { //this portion works fine
text = text.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3");
return text;
}else if (text.length > 10) { //this is where I need help
text = text.replace(/(\d{3})(\d{3})(\d{4})(\d{[1-5]})/, "($1) $2-$3 x$4");
return text;
}
});

是否有正则表达式来处理这里的数字范围?

最佳答案

是的,省略方括号并使用逗号。

\d{1,5}

关于Javascript正则表达式数值范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17453059/

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