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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM