目标:前端实现多证件显示数据脱敏
phoneNumberDesensitization(phone){
phone = phone.replace(/(\d{3})\d{4}(\d{4})/,"$1***********$2");
return phone;
},
passportDesensitization(passport){
var passportmatch = passport.match(/^1[45][0-9]{7}|([P|p|S|s]\d{7})|([S|s|G|g|E|e]\d{8})|([Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\d{8})|([H|h|M|m]\d{8,10})$/);
if (passportmatch != null){
const p = passportmatch.filter(function () {
return true;
})[0];
passport=passport.replace(p,p.substring(0,1)+"*******"+p.substring(p.length-1,p.length))
}
return passport;
},
gaPassportDesensitization(GApassport){
var GAmatch=GApassport.match(/([H|M]\d{8})/);
if (GAmatch!=null){
const ga=GAmatch[0];
GApassport=GApassport.replace(ga,ga.substring(0,1)+"********"+ga.substring(ga.length-1,ga.length))
}
return GApassport;
},
tPassportDesensitization(Tpassport){
var Tmatch=Tpassport.match(/\d{8}|\d{18}/);
if (Tmatch!=null){
const t = Tmatch.filter(function () {
return true;
})[0];
Tpassport=Tpassport.replace(t,t.substring(0,2)+"*****"+t.substring(t.length-1,t.length))
}
return Tpassport;
},
foreignerDesensitization(foreigner){
var fore=foreigner.match(/[A-Z]{3}\d{12}/);
if (fore!=null){
const f = fore.filter(function () {
return true;
})[0];
foreigner=foreigner.replace(f,f.substring(0,3)+"***********"+f.substring(f.length-1,f.length))
}
return foreigner;
},
identityDesensitization(identityCard){
let reg = new RegExp("(?<=\\d{3})\\d{12}(?=\\d{2})","g");
return identityCard.replace(reg, "************")
},
ocrDesensitization(str){
str=this.identityDesensitization(str);
str=this.foreignerDesensitization(str);
str=this.gaPassportDesensitization(str);
str=this.passportDesensitization(str);
str=this.tPassportDesensitization(str);
return str;
}