Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 格式化电话号码_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 格式化电话号码

Javascript 格式化电话号码,javascript,jquery,regex,Javascript,Jquery,Regex,从数据库获取电话号码后,下面的文本框将打印该号码的值 <table> <tr> <td> <s:textfield theme="simple" name="phoneNumber"/> </td> </tr> </table> 如何以(xxx)xxx xxxx格式打印此值。 注意:这些值以数据库中的0123456789形式提供 输出应该是(012)345-6789。我更喜欢使用replace和regexp

从数据库获取电话号码后,下面的文本框将打印该号码的值

<table>
<tr>
<td>
<s:textfield theme="simple" name="phoneNumber"/>
</td>
</tr>
</table>

如何以(xxx)xxx xxxx格式打印此值。 注意:这些值以数据库中的0123456789形式提供
输出应该是(012)345-6789。

我更喜欢使用
replace
regexp
(代码越少,功能越多)

参考资料:

const formatPhoneNumber=(phoneNumber)=>{
//将原始数字转换为(xxx)xxx格式
const x=phoneNumber&&phoneNumber.replace(/\D/g',).match(/(\D{0,3})(\D{0,3})(\D{0,4})/);
return!x[2]?x[1]:`(${x[1]})${x[2]}${x[3]?`-${x[3]}`:'''}`;
};

日志(格式化电话号码(“1111111”)什么是服务器端语言?在服务器端格式化数字要容易得多。我使用的是struts2 framwork和java语言。在javascript中格式化字符串:数据库中的数字都是相同的格式吗?或者您是否有+41792359866或0041792359866或+41-792-349-866(即日语格式vs nanp)之类的数字它们的格式都是0123456789您的表达式正在生成异常表达式“(“+phone.substring(0,3)+”)本身就是一个例外。返回空值。请尝试将电话变量转换为字符串。它应如下所示:格式化的_phone='('+phone.substr(0,3)+'+phone.substr(3,3)+'-'+phone.substr(6,4);
phone = "0123456789"
formated_phone = "("+phone.substring(0,3)+")"+phone.substring(3,6)+"-"+phone.substring(6,11)
         var phone = "0123456789";
         phone.replace(/(\d{3})(\d{3})(\d{4})/,"($1)$2-$3"); // (012)345-6789