格式化电话号码

Nit*_*ish 4 javascript regex jquery

下面的文本框正在打印从数据库中获取电话号码后的值。

<table>
<tr>
<td>
<s:textfield theme="simple" name="phoneNumber"/>
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)

如何以 (xxx) xxx-xxxx 格式打印此值。注意:这些值以 0123456789 的形式来自数据库,输出应为 (012)345-6789。

ant*_*njs 8

我更喜欢使用replaceregexp(更少的代码,更多的功能)。

         var phone = "0123456789";
         phone.replace(/(\d{3})(\d{3})(\d{4})/,"($1)$2-$3"); // (012)345-6789
Run Code Online (Sandbox Code Playgroud)

参考:https : //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace


Dan*_*kov 2

phone = "0123456789"
formated_phone = "("+phone.substring(0,3)+")"+phone.substring(3,6)+"-"+phone.substring(6,11)
Run Code Online (Sandbox Code Playgroud)