/** * 隐藏手机号码 * @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位的数字
原文地址:https://www.cnblogs.com/ympjsc/p/12287190.html
时间: 2024-10-31 15:15:50