js将手机号中间四位变成*号

方法一、利用数组splice,split,join方法

var tel = 18810399133;
tel = "" + tel;
var ary = tel.split("");
ary.splice(3,4,"****");
var tel1=ary.join("");
console.log(tel1);

方法二、利用字符串的substr方法

var tel = 18810399133;
tel = "" + tel;
var tel1 = tel.substr(0,3) + "****" + tel.substr(7)
console.log(tel1);

方法三、利用字符串substring方法

var tel = 18810399133;
tel = "" + tel;
var tel1 =tel.replace(tel.substring(3,7), "****")
console.log(tel1);

方法四、利用正则

var tel = 18810399133;
tel = "" + tel;
var reg=/(\d{3})\d{4}(\d{4})/;
var tel1 = tel.replace(reg, "$1****$2")
console.log(tel1);
posted @ 2020-05-12 17:09  SunB  阅读(434)  评论(0编辑  收藏  举报