字符串加密、空格

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* @param str 接收的字符串(手机号/身份证号/银行卡号/中文)
* @param start 开头展示字符数
* @param end 结束展示字符数
* @param maskChar 加密符号
* @returns string
*/

// 通用加密
const encryption = (str = "", start = 3, end = 2, maskChar = "*") => {
if (!str) return str;
let toStr = str.toString();
toStr = toStr.replace(/\s*/g, "");
start = Math.max(start, 0);
end = toStr.length - Math.min(end, toStr.length);
const maskedPart = maskChar.repeat(end - start);
return toStr.substring(0, start) + maskedPart + toStr.substring(end);
};

// 任意字符串4位一空格
const space4 = value => {
if (!value) return "";
let target = value.replace(/\s*/g, "").replace(/(.{4})(?=.)/g, "$1 ");
return target; // xxxx 8888 xxxx 张三i8 8
};

// 证件号分割6444
const space_idCard = value => {
if (!value) return value;
let target = value.replace(/\s*/g, "").replace(/(.{6})(.{4})(.{4})/, "$1 $2 $3 ");
return target; // 410323 1900 0101 010X
};

// 证件号加密6*4
const encry_idCard = value => {
if (!value) return value;
let target = encryption(value, 6, 4);
return target; // 410323********010X
};

// 证件号分割加密:前边6位,后边4位,中间8位并且带空格
const spaceEncry_idCard = value => {
if (!value) return value;
let target = value.replace(/\s*/g, "").replace(/^([a-zA-Z0-9]{6})[a-zA-Z0-9]+([a-zA-Z0-9]{4})$/, "$1 **** **** $2");
return target; // 410323 **** **** 010X
};

// 手机号分割344
const space_phone = value => {
if (!value) return value;
let target = value.replace(/\s*/g, "").replace(/(.{3})(.{4})/, "$1 $2 ");
return target; // 133 3383 3333
};

// 手机号加密3*4
const encry_phone = value => {
if (!value) return value;
let target = encryption(value, 3, 4);
return target; // 133****4444
};

// 手机号分割加密3 * 4
const spaceEncry_phone = value => {
if (!value) return value;
let target = value.replace(/\s*/g, "").replace(/^(\d{3})\d+(\d{4})$/, "$1 **** $2");
return target; //133 **** 3333
};

// 银行卡号分割
const space_bankCard = value => {
if (!value) return "";
let target = value.replace(/\s*/g, "").replace(/(\d{4})(?=\d)/g, "$1 ");
return target; // 6221 8888 8888 8888 88
};

// 银行卡号加密
const encry_bankCard = value => {
if (!value) return "";
let target = encryption(value, 4, 4);
return target; // 6221********8888
};

// 银行卡号分割加密
const spaceEncry_bankCard = value => {
if (!value) return "";
let target = value.replace(/\s*/g, "").replace(/^(\d{4})\d+(\d{4})$/, "$1 **** **** $2");
return target; // 6221 **** **** 8888
};

// 姓名名字加密(加密名字或中间)
const encry_endNameA = value => {
let target = encryption(value, 1, value.length > 2 ? 1 : 0);
return target; // 张三、张三星、tony:张*、张*星、t**y
};

// 姓名名字加密(只显示姓,加密名字所有字)
const encry_endNameB = value => {
let target = encryption(value, 1, 0);
return target; // 张三、张三星、tony:张*、张**、t***
};

// 姓名只显示最后一个字
const encry_startName = value => {
if (!value) return "";
let target = value.replace(/.(?=.)/g, "*");
return target; // 张三、张三星、tony:*三、**星、***y
};

const encry = {
encryption,
space4,
space_idCard,
encry_idCard,
spaceEncry_idCard,
space_phone,
encry_phone,
spaceEncry_phone,
space_bankCard,
encry_bankCard,
spaceEncry_bankCard,
encry_endNameA,
encry_endNameB,
encry_startName
};
export default encry;

更新于

请我喝[茶]~( ̄▽ ̄)~*

Ming Liu 微信支付

微信支付

Ming Liu 支付宝

支付宝

Ming Liu 贝宝

贝宝