手机号隐藏中间4位变成****

/**
 * 手机号隐藏中间4位变成****
 **/
export function phone(num) {
  num = "" + num;
  let tel1 =num.replace(num.substring(3,7), "****");
 return tel1;
}

 上述该方法再遇到一般的手机号都可以的,但是如果遇到如:13813813465,这种手机号时,就会变成****3813485这样的。
另一种方法:我目前用的是这种的方法,

/**
 * 手机号隐藏中间4位变成****
 **/
export function phone(num) {
  num = "" + num;
  let reg=/(d{3})d{4}(d{4})/;
  let tel1 = num.replace(reg, "$1****$2")
  return tel1;
}
原文地址:https://www.cnblogs.com/mxyr/p/10768116.html