-
Notifications
You must be signed in to change notification settings - Fork 0
/
replaceAll.js
94 lines (89 loc) · 3.62 KB
/
replaceAll.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
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
//replaceAll library js by Aahedi https://github.com/aahedi/replaceAll
function replaceAll(str, find, replace, options) {
var gx = 'g';
var regExp = new RegExp('\\b' + find + '\\b','g');
var new_replace = replace;
var new_find = find.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&'); //escapeRegex
if(options===undefined){
return str.replace(new RegExp(new_find, gx), replace);
}else{
var opt = ['prefix_is','suffix_is','prefix_not','suffix_not','insensitive'];
for (let i = 0; i < opt.length; i++) {
if(options[opt[i]]===undefined){
options[opt[i]] = '';
}else{
if(Object.prototype.toString.call(options[opt[i]]) === "[object String]"){ //https://stackoverflow.com/a/17772086
options[opt[i]] = options[opt[i]].replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&'); //escapeRegex
}
}
}
if(options.insensitive === true){
gx = 'gi';
}else{
gx = 'g';
}
if(options.prefix_is.length > 1 && options.suffix_is.length > 1){
if(options.prefix_not.length > 1 && options.suffix_not.length > 1){
regExp = '(?<!('+options.prefix_not+'))('+new_find+')(?!('+options.suffix_not+'))';
new_replace = replace;
}else if(options.prefix_not.length > 1){
regExp = '(?<!('+options.prefix_not+'))('+new_find+')('+options.suffix_is+')';
new_replace = replace+'$3';
}else if(options.suffix_not.length > 1){
regExp = '('+options.prefix_is+')('+new_find+')(?!('+options.suffix_not+'))';
new_replace = '$1'+replace;
}else{
regExp = '('+options.prefix_is+')('+new_find+')('+options.suffix_is+')';
new_replace = '$1'+replace+'$3';
}
}
else if(options.prefix_is.length > 1){
if(options.prefix_not.length > 1 && options.suffix_not.length > 1){
regExp = '(?<!('+options.prefix_not+'))('+new_find+')(?!('+options.suffix_not+'))';
new_replace = replace;
}else if(options.prefix_not.length > 1){
regExp = '(?<!('+options.prefix_not+'))('+new_find+')';
new_replace = replace;
}else if(options.suffix_not.length > 1){
regExp = '('+options.prefix_is+')('+new_find+')(?!('+options.suffix_not+'))';
new_replace = '$1'+replace;
}else{
regExp = '('+options.prefix_is+')('+new_find+')';
new_replace = '$1'+replace;
}
}
else if(options.suffix_is.length > 1){
if(options.prefix_not.length > 1 && options.suffix_not.length > 1){
regExp = '(?<!('+options.prefix_not+'))('+new_find+')(?!('+options.suffix_not+'))';
new_replace = replace;
}else if(options.prefix_not.length > 1){
regExp = '(?<!('+options.prefix_not+'))('+new_find+')('+options.suffix_is+')';
new_replace = replace+'$3';
}else if(options.suffix_not.length > 1){
regExp = '('+new_find+')(?!('+options.suffix_not+'))';
new_replace = replace;
}else{
regExp = '('+new_find+')('+options.suffix_is+')';
new_replace = replace+'$2';
}
}
else{
if(options.prefix_not.length > 1 && options.suffix_not.length > 1){
regExp = '(?<!('+options.prefix_not+'))('+new_find+')(?!('+options.suffix_not+'))';
new_replace = replace;
}else if(options.prefix_not.length > 1){
regExp = '(?<!('+options.prefix_not+'))('+new_find+')';
new_replace = replace;
}else if(options.suffix_not.length > 1){
regExp = '('+new_find+')(?!('+options.suffix_not+'))';
new_replace = replace;
}else{
regExp = '('+new_find+')';
new_replace = replace;
}
}
return str.replace(new RegExp(regExp, gx), new_replace);
}
}
//example: replaceAll('123hedi456 abchediefg 123hedi abchedi hedi456 hediefg 123Hedi456', 'hedi', '****', {prefix_is:'123',suffix_is:'456',prefix_not:'123',suffix_not:'456',insensitive:true})
//result: '123hedi456 abc****efg 123hedi abc**** hedi456 ****efg 123Hedi456';