-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
45 lines (34 loc) · 807 Bytes
/
script.js
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
const data = {
string: 'VIVO-(1234) a1:b2:c3:d4:e5:f6',
regex: '\\w+:\\w+:(\\w+):(\\w+):\\w+:\\w+',
replace: '$2$3$1',
upper: '.+',
}
const re = (str) => new RegExp(str, 'g');
function ruler(str, data) {
let value;
const run = (pattern, func) => {
const reg = re(pattern);
return str.replace(reg, func);
};
const _upper = (s) => s.toUpperCase();
const _lower = (s) => s.toLowerCase();
if (data.upper) {
value = run(data.upper, _upper);
}
if (data.lower) {
value = run(data.lower, _lower);
}
return value;
}
const gen = (data) => {
let { string, regex, replace } = data;
const reg = re(regex);
const sub = replace;
if (reg.test(string)) {
const str = string.replace(reg, sub);
return ruler(str, data);
}
}
// Return: C3D41234
gen(data)