方法一: 字符串截取+拼接
var phone = '13500001111';
phone.substr(0, 3) + '****' + phone.substr(7, 11);
//复用封装
function formatPhone(phone) {
if (typeof phone == 'number') {
phone = phone.toString();
}
return phone.substr(0, 3) + '****' + phone.substr(7, 11);
}
方法二:正则表达式替换
function formatPhone(phone) {
return phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
}