1
|
|
|
/** global: UB */ |
2
|
|
|
|
3
|
|
|
var stringFuncs = { |
4
|
|
|
|
5
|
|
|
replaceAllCI: function(find, replace){ |
6
|
|
|
return this.replaceAll(find, replace, false, false); |
7
|
|
|
}, |
8
|
|
|
replaceAll: function(find, replace, wholeWords = false, caseSensitive = true){ |
9
|
|
|
var s = this.toString(); |
10
|
|
|
|
11
|
|
|
// find and replace given term |
12
|
|
|
if (wholeWords || !caseSensitive) { |
13
|
|
|
var regex = UB.regex.New(find, wholeWords, caseSensitive, true); |
14
|
|
|
s = s.replace(regex, replace); |
15
|
|
|
}else { |
16
|
|
|
if (s.indexOf(find) > -1) { |
17
|
|
|
return s.split(find).join(replace); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
return s; |
21
|
|
|
}, |
22
|
|
|
replaceOnce: function(find, replace, wholeWords = false, caseSensitive = true){ |
23
|
|
|
var s = this.toString(); |
24
|
|
|
var regex = UB.regex.New(find, wholeWords, caseSensitive, false); |
25
|
|
|
if (s.match(regex) != null){ |
26
|
|
|
s = s.replace(regex, replace); |
27
|
|
|
} |
28
|
|
|
return s; |
29
|
|
|
}, |
30
|
|
|
replaceBetween: function(findStart, findEnd, replaceWith, keepEnds = true, startAt = 0, times = 0){ |
31
|
|
|
var s = this.toString(); |
32
|
|
|
|
33
|
|
|
var start; |
34
|
|
|
var last = 0; |
35
|
|
|
var done = 0; |
36
|
|
|
|
37
|
|
|
if ((start = s.indexOf(findStart)) > -1){ |
38
|
|
|
|
39
|
|
|
// for each instance of marker |
40
|
|
|
while (true) { |
41
|
|
|
|
42
|
|
|
// exit if end marker not in text |
43
|
|
|
var end = s.indexOf(findEnd, start + findStart.length); |
44
|
|
|
if (end === -1) { |
45
|
|
|
return s; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// get code before/after markers |
49
|
|
|
var partA = s.substr(0, start); |
50
|
|
|
var partB = s.substr(end + findEnd.length); |
51
|
|
|
|
52
|
|
|
// merge code with new value inplace of markers |
53
|
|
|
if (keepEnds) { |
54
|
|
|
s = partA + findStart + replaceWith + findEnd; |
55
|
|
|
}else { |
56
|
|
|
s = partA + replaceWith; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// move forward |
60
|
|
|
last = s.length; |
61
|
|
|
|
62
|
|
|
// add ending text |
63
|
|
|
s += partB; |
64
|
|
|
|
65
|
|
|
// find next marker |
66
|
|
|
start = s.indexOf(findStart, last); |
67
|
|
|
if (start === -1) { |
68
|
|
|
break; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// only replace certain number of times |
72
|
|
|
done++; |
73
|
|
|
if (done == times) { /// ignore "times" if 0 |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
return s; |
80
|
|
|
}, |
81
|
|
|
replaceMany: function(findArray, replaceArray, wholeWords = false, caseSensitive = true){ |
82
|
|
|
var result = this.toString(); |
83
|
|
|
if (findArray.length != replaceArray.length){ |
84
|
|
|
return result; |
85
|
|
|
} |
86
|
|
|
// find and replace each term |
87
|
|
|
for (var f = 0, fl = findArray.length;f<fl;f++){ |
88
|
|
|
if (!caseSensitive || result.indexOf(findArray[f]) > -1) { |
89
|
|
|
result = result.replaceAll(findArray[f], replaceArray[f], wholeWords, caseSensitive); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return result; |
93
|
|
|
}, |
94
|
|
|
replaceManyObj: function(findReplace, wholeWords = false, caseSensitive = true){ |
95
|
|
|
var result = this.toString(); |
96
|
|
|
|
97
|
|
|
for (var ft in findReplace){ |
|
|
|
|
98
|
|
|
var rt = findReplace[ft]; |
99
|
|
|
|
100
|
|
|
if (!caseSensitive || result.indexOf(ft) > -1) { |
101
|
|
|
|
102
|
|
|
result = result.replaceAll(ft, rt, wholeWords, caseSensitive); |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return result; |
109
|
|
|
}, |
110
|
|
|
replaceManyWithOne: function(findArray, replace, wholeWords = false, caseSensitive = true){ |
111
|
|
|
var s = this.toString(); |
112
|
|
|
|
113
|
|
|
// find and replace each term |
114
|
|
|
for (var f = 0, fl = findArray.length;f<fl;f++){ |
115
|
|
|
s = s.replaceAll(findArray[f], replace, wholeWords, caseSensitive); |
116
|
|
|
} |
117
|
|
|
return s; |
118
|
|
|
}, |
119
|
|
|
replaceTimes: function(find, replaceWith, startAt = 0, forward = true, times = 0){ |
120
|
|
|
var s = this.toString(); |
121
|
|
|
|
122
|
|
|
var start; |
123
|
|
|
var last = startAt; |
124
|
|
|
var done = 0; |
125
|
|
|
|
126
|
|
|
// how long is the replaced string |
127
|
|
|
var len = replaceWith.length; |
128
|
|
|
|
129
|
|
|
// for each instance of marker |
130
|
|
|
while ((start = forward ? s.indexOf(find, last) : s.lastIndexOf(find, last)) > -1) { |
131
|
|
|
|
132
|
|
|
// get code before/after markers |
133
|
|
|
var partA = s.substr(0, start); |
134
|
|
|
var partB = s.substr(s.indexOf(find, start) + find.length); |
135
|
|
|
|
136
|
|
|
// merge code with new value inplace of markers |
137
|
|
|
s = partA + replaceWith + partB; |
138
|
|
|
|
139
|
|
|
// move forward/backward |
140
|
|
|
last = start + (forward ? len : -1); |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
// only replace certain number of times |
144
|
|
|
done++; |
145
|
|
|
if (done == times) { /// ignore "times" if 0 |
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
return s; |
150
|
|
|
}, |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** Checks if the prefix is `oldPrefix`, and replaces with `newPrefix` if it is. |
154
|
|
|
Does NOT replace chars in the prefix! Replaces the ENTIRE prefix instead! */ |
155
|
|
|
replacePrefix: function(oldPrefix, newPrefix, caseSensitive = true){ |
156
|
|
|
var str = this; |
157
|
|
|
if (str.startsWith(oldPrefix, caseSensitive)) { |
158
|
|
|
return newPrefix + str.substr(oldPrefix.length); |
159
|
|
|
} |
160
|
|
|
return str; |
161
|
|
|
}, |
162
|
|
|
/** Checks if the prefix is `oldPrefix`, and replaces with `newPrefix` if it is. |
163
|
|
|
Does NOT replace chars in the prefix! Replaces the ENTIRE prefix instead! */ |
164
|
|
|
replacePostfix: function(oldPostfix, newPostfix, caseSensitive = true){ |
165
|
|
|
var str = this; |
166
|
|
|
if (str.endsWith(oldPostfix, caseSensitive)) { |
167
|
|
|
return str.substr(0, str.length - oldPostfix.length) + newPostfix; |
168
|
|
|
} |
169
|
|
|
return str; |
170
|
|
|
}, |
171
|
|
|
|
172
|
|
|
none:null |
173
|
|
|
}; |
174
|
|
|
|
175
|
|
|
// register funcs |
176
|
|
|
UB.registerFuncs(String.prototype, stringFuncs); |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: