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

mac2022-06-30  26

方法一、利用数组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);

 

转载于:https://www.cnblogs.com/chengkun101/p/7879522.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)