隐藏手机号

/**
 * 隐藏手机号码
 * @param phone 手机号码
 * @param startIndex 从第几个数字开始隐藏
 * @param endIndex 隐藏至第几个数字
 */
function hiddenPhone(phone, startIndex = 4, endIndex = 7) {
  if (typeof phone !== 'string') {
    return '';
  }
  const maxLength = 11;
  const fontLength = startIndex - 1;
  const endLength = maxLength - endIndex;
  const hiddenLength = maxLength - fontLength - endLength;
  const reg = new RegExp(`(\\d{${fontLength}})(\\d{${hiddenLength}})(\\d{${endLength}})`);
  let star = '';
  len = endIndex-startIndex +1;
  for(let i = 0;i< len;i++) {
    star += '*'
  }
  return phone.replace(reg, `$1${star}$3`);
}

默认隐藏第4-7位的数字

 

 



猜你喜欢

转载自www.cnblogs.com/ympjsc/p/12287190.html
今日推荐