文章目录
-
- 脱敏公用
- 中文姓名脱敏
- 证件脱敏
- 卡(社保卡,医保卡)脱敏
- 手机号脱敏
- 固定电话
- 邮箱脱敏
- 地址脱敏
- 昵称脱敏
- 车辆信息脱敏
- 支付宝、微信号、QQ号脱敏
- 完整方法
脱敏公用
class JSSDK {static desensitizedCommon (str, begin, end) {if (!str && (begin + end) >= str.length) {return "";}let leftStr = str.substring(0, begin);let rightStr = str.substring(str.length - end, str.length);let strCon = ''for (let i = 0; i < str.length - end - begin; i++) {strCon += '*';}return leftStr + strCon + rightStr;}}
中文姓名脱敏
name: name => {if (name.length === 2) {return name.replace(/^./, '*')}else if (name.length === 3) {return name.replace(/^./, '*')}else if (name.length <= 6 && name.length >= 3) {return name.replace(name.substr(0, name.length - 2), '***')}else if (name.length > 6) {return name.replace(/^(.).*(.)$/, '$1*****$2')}}
证件脱敏
IdNumber: IdNumber => {if (IdNumber.length === 18) {return IdNumber.replace(/^(.).*(.)$/, '$1***********$2')}else if (IdNumber.length === 10) {return IdNumber.replace(/^(.).*(.)$/, '$1********$2')}else if (IdNumber.length === 9) {return IdNumber.replace(/^(.).*(.)$/, '$1********$2')}}
卡(社保卡,医保卡)脱敏
card: card => {if (card.length % 3 === 0) {return card.replace(/^(.).*(.)$/, '$1$1$1$1*****$2$2$2')} else {return card.replace(/^(.).*(.)$/, '$1$1$1******$2$2$2')}},
手机号脱敏
phone: phone => {if (phone.length === 11) {return phone.substring(0, 3) + '****' + phone.substring(7, 11)}else if (phone.length === 8) {return phone.substring(0, 2) + '****' + phone.substring(6, 8)}else if (phone.length === 9) {return phone.substring(0, 2) + '****' + phone.substring(6, 9)}else {return phone.substring(0, 3) + '****' + phone.substring(8, 11)}}
固定电话
iphone: iphone => {return iphone.substring(0, 3) + '****' + iphone.substring(6, 11)}
邮箱脱敏
email: email => {return email.substr(0, 3) + '****' + email.substr(email.indexOf('@'))},
地址脱敏
address: address => {return address.substr(0, address.indexOf('区') + 1) + '****'}
昵称脱敏
nickname: nickname => {return nickname.replace(/^(.).*(.)$/, '$1*****$2')}
车辆信息脱敏
carNumber: carNumber => {return carNumber.replace(/^(.).*(.)$/, '$1*****$2')}
支付宝、微信号、QQ号脱敏
alipay: alipay => {return alipay.replace(/.(?=.{4})/g, '*')}
完整方法
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><meta http-equiv="X-UA-Compatible" content="IE=edge" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Document</title></head><body><script>const tools = {name: name => {if (name.length === 2) {return name.replace(/^./, '*')}else if (name.length === 3) {return name.replace(/^./, '*')}else if (name.length <= 6 && name.length >= 3) {return name.replace(name.substr(0, name.length - 2), '***')}else if (name.length > 6) {return name.replace(/^(.).*(.)$/, '$1*****$2')}},IdNumber: IdNumber => {if (IdNumber.length === 18) {return IdNumber.replace(/^(.).*(.)$/, '$1***********$2')}else if (IdNumber.length === 10) {return IdNumber.replace(/^(.).*(.)$/, '$1********$2')}else if (IdNumber.length === 9) {return IdNumber.replace(/^(.).*(.)$/, '$1********$2')}},card: card => {if (card.length % 3 === 0) {return card.replace(/^(.).*(.)$/, '$1$1$1$1*****$2$2$2')} else {return card.replace(/^(.).*(.)$/, '$1$1$1******$2$2$2')}},phone: phone => {if (phone.length === 11) {return phone.substring(0, 3) + '****' + phone.substring(7, 11)}else if (phone.length === 8) {return phone.substring(0, 2) + '****' + phone.substring(6, 8)}else if (phone.length === 9) {return phone.substring(0, 2) + '****' + phone.substring(6, 9)}else {return phone.substring(0, 3) + '****' + phone.substring(8, 11)}},iphone: iphone => {return iphone.substring(0, 3) + '****' + iphone.substring(6, 11)},email: email => {return email.substr(0, 3) + '****' + email.substr(email.indexOf('@'))},address: address => {return address.substr(0, address.indexOf('区') + 1) + '****'},nickname: nickname => {return nickname.replace(/^(.).*(.)$/, '$1*****$2')},carNumber: carNumber => {return carNumber.replace(/^(.).*(.)$/, '$1*****$2')},alipay: alipay => {return alipay.replace(/.(?=.{4})/g, '*')}}console.log(tools.name('张三疯')) console.log(tools.name('张鹏')) console.log(tools.name('张鹏的姓名')) console.log(tools.name('张鹏的姓名的姓名')) console.log(tools.IdNumber('622422000080888889')) console.log(tools.IdNumber('3208888888')) console.log(tools.IdNumber('P12345678')) console.log(tools.card('1234567890123456789')) console.log(tools.card('123456789012345678')) console.log(tools.phone((phoneStr = '13000000000'))) console.log(tools.phone((phoneStr = '12345678'))) console.log(tools.phone((phoneStr = '123456789'))) console.log(tools.iphone('010-1234567')) console.log(tools.email('1482516333@qq.com')) console.log(tools.address('湖北省武汉市武昌区68号路301')) console.log(tools.nickname('人情淡薄纸一张')) console.log(tools.carNumber('粤B12345')) console.log(tools.alipay('18852638000')) </script></body>
</html>