var identifynumber = "513436200003276211"; //身份证号 identifynumber = identifynumber.replace(/(?<=d{3})d{12}(?=d{2}[0-9A-Z]{1})/, "************"); console.log(identifynumber); // 513************211 var phoneNum = "18620792644"; //手机号 phoneNum = phoneNum.replace(/(?<=d{3})d{4}(?=d{4})/, "****"); console.log(phoneNum); //186****2644
replace正则表达式替换方法,苹果手机不兼容。使用substring()截取的方式
var identifynumber = "513436200003276211"; //身份证号 identifynumber = identifynumber.substring(0,3) + "************"+identifynumber.substring(15,18); console.log(identifynumber); // 513************211 var phoneNum = "18620792644"; //手机号 phoneNum = phoneNum.substring(0,3)+ "****" + phoneNum.substring(8,11); //186****2644