下面的文本框打印从数据库中获取的电话号码的值。
<table>
<tr>
<td>
<s:textfield theme="simple" name="phoneNumber"/>
</td>
</tr>
</table>
如何以 (xxx) xxx-xxxx 格式打印此值。 注意:值以 0123456789 的形式来自数据库 输出应该是 (012)345-6789。
最佳答案
我更愿意使用replace
和regexp
(代码更少,功能更多)。
var phone = "0123456789";
phone.replace(/(\d{3})(\d{3})(\d{4})/,"($1)$2-$3"); // (012)345-6789
引用:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
关于javascript - 格式化电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9973955/