js手机中间四位替换成*号

方法一:    字符串截取+拼接

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");
       }

 

上一篇:SQL注入漏洞基础拓展


下一篇:【牛客网】数一数