银行卡号保留头尾,星号隐藏中间

1,093 阅读1分钟

保留前后四位 中间每4个*会有一个空格 6212 **** **** *** 0222

hideBankCard (value){
    if (value && value.length > 8) {
      return `${value.substring(0, 4)} ${"*".repeat(value.length - 8).replace(/(.{4})/g, `$1 `)}${value.length % 4 ? " " : ""}${value.slice(-4)}`;
    }
    return value;
  },

保留前后四位 中间全部用*号代替 不包含空格

export const number=(value)=>{
  if(value&&value.length>8) {
    let reg = /^(\d{4})(\d*)(\d{4})$/;
    let str = value.replace(reg, (a, b, c, d)=> {
      return b + c.replace(/\d/g, "*") + d;
    });
    return str
  }else {
    return value
  }
}
 
console.log(number('6212261611110000222'));

转载自:blog.csdn.net/qq_45062261…