工作中遇到要将系统中的手机号和身份证号等信息进行脱敏,于是自己封装了个通用的方法:
function Desensitization(str,beginLen,endLen) {
var len = str.length;
var firstStr = str.substr(0,beginLen);
var lastStr = str.substr(endLen);
var middleStr = str.substring(beginLen, len-Math.abs(endLen)).replace(/[\s\S]/ig, ‘*‘);
tempStr = firstStr + middleStr + lastStr;
return tempStr;
}
Desensitization(‘18353246789097654‘,4,-4);