1
|
|
|
|
2
|
|
|
var stringFuncs = { |
3
|
|
|
|
4
|
|
|
// char type |
5
|
|
|
isOfCharRange: function(type){ |
6
|
|
|
var char = this; |
7
|
|
|
|
8
|
|
|
// needs to be exactly 1 char! |
9
|
|
|
if (char.length != 1) { |
|
|
|
|
10
|
|
|
return false; |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
// check type |
14
|
|
|
if (type == charCharRange.digit) { |
|
|
|
|
15
|
|
|
var code = char.charCodeAt(0); |
16
|
|
|
return code >= 48 && code <= 57; |
17
|
|
|
} |
18
|
|
|
if (type == charCharRange.letter) { |
19
|
|
|
var code = char.charCodeAt(0); |
|
|
|
|
20
|
|
|
if (code >= 65 && code <= 90){ |
21
|
|
|
return true; |
22
|
|
|
} |
23
|
|
|
if (code >= 97 && code <= 122){ |
24
|
|
|
return true; |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
if (type == charCharRange.space) { |
28
|
|
|
return char == " " || char == "\t" || char == "\r" || char == "\n"; |
29
|
|
|
} |
30
|
|
|
if (type == charCharRange.symbol) { |
31
|
|
|
return symbolsCommon.indexOf(char) > -1; |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return false; |
35
|
|
|
}, |
36
|
|
|
isNoneOfCharRange: function(type){ |
37
|
|
|
var str = this; |
38
|
|
|
return !str.containsCharRange(type); |
39
|
|
|
}, |
40
|
|
|
isAllOfCharRange: function(type, ifNoChars = false){ |
41
|
|
|
var str = this; |
42
|
|
|
|
43
|
|
|
// exit quickly if blank string |
44
|
|
|
if (str == null || str.length == 0) { |
|
|
|
|
45
|
|
|
return ifNoChars; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// find char of type |
49
|
|
|
for (var c = 0, cl = str.length;c<cl;c++){ |
50
|
|
|
if (!str.charAt(c).isOfCharRange(type)) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
return true; |
55
|
|
|
}, |
56
|
|
View Code Duplication |
isAllOfCharRange2: function(type1, type2, ifNoChars = false){ |
|
|
|
|
57
|
|
|
var str = this; |
58
|
|
|
|
59
|
|
|
// exit quickly if blank string |
60
|
|
|
if (str == null || str.length == 0) { |
|
|
|
|
61
|
|
|
return ifNoChars; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// find char of type |
65
|
|
|
for (var c = 0, cl = str.length;c<cl;c++){ |
66
|
|
|
if (!(str.charAt(c).isOfCharRange(type1) || str.charAt(c).isOfCharRange(type2))) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
return true; |
71
|
|
|
}, |
72
|
|
|
containsCharRange: function(type, startAt = 0, ifNoChars = false){ |
73
|
|
|
var str = this; |
74
|
|
|
|
75
|
|
|
// exit quickly if blank string |
76
|
|
|
if (str == null || str.length == 0) { |
|
|
|
|
77
|
|
|
return ifNoChars; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// find char of type |
81
|
|
|
for (var c = startAt, cl = str.length;c<cl;c++){ |
82
|
|
|
if (str.charAt(c).isOfCharRange(type)) { |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
return false; |
87
|
|
|
}, |
88
|
|
|
startsWithCharRange: function(type, ifNoChars = false){ |
89
|
|
|
var str = this; |
90
|
|
|
|
91
|
|
|
// exit quickly if blank string |
92
|
|
|
if (str == null || str.length == 0) { |
|
|
|
|
93
|
|
|
return ifNoChars; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// check if begins with char of type |
97
|
|
|
return str.charAt(0).isOfCharRange(type); |
98
|
|
|
}, |
99
|
|
|
endsWithCharRange: function(type, ifNoChars = false){ |
100
|
|
|
var str = this; |
101
|
|
|
|
102
|
|
|
// exit quickly if blank string |
103
|
|
|
if (str == null || str.length == 0) { |
|
|
|
|
104
|
|
|
return ifNoChars; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// check if ends with char of type |
108
|
|
|
return str.charAt(str.length - 1).isOfCharRange(type); |
109
|
|
|
}, |
110
|
|
View Code Duplication |
indexOfCharRange: function(type, startAt = null, forwards = true){ |
|
|
|
|
111
|
|
|
var str = this; |
112
|
|
|
|
113
|
|
|
// exit quickly if blank string |
114
|
|
|
if (str == null || str.length == 0) { |
|
|
|
|
115
|
|
|
return -1; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// find char of type |
119
|
|
|
if (forwards) { |
120
|
|
|
if (startAt == null) { |
|
|
|
|
121
|
|
|
startAt = 0; |
122
|
|
|
} |
123
|
|
|
for (var c = startAt, cl = str.length;c<cl;c++){ |
124
|
|
|
if (str.charAt(c).isOfCharRange(type)) { |
125
|
|
|
return c; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
}else { |
129
|
|
|
if (startAt == null) { |
130
|
|
|
startAt = str.length - 1; |
131
|
|
|
} |
132
|
|
|
for (var c = startAt;c >= 0;c--){ |
|
|
|
|
133
|
|
|
if (str.charAt(c).isOfCharRange(type)) { |
134
|
|
|
return c; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
return -1; |
139
|
|
|
}, |
140
|
|
|
lastIndexOfCharRange: function(type){ |
141
|
|
|
var str = this; |
142
|
|
|
return str.indexOfCharRange(type, null, false); |
143
|
|
|
}, |
144
|
|
|
restrictToCharRange: function(type, startAt = 0){ |
145
|
|
|
var str = this; |
146
|
|
|
|
147
|
|
|
// exit quickly if blank string |
148
|
|
|
if (str == null || str.length == 0) { |
|
|
|
|
149
|
|
|
return ""; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// extract all chars of given type |
153
|
|
|
var output = []; |
154
|
|
|
for (var c = startAt, cl = str.length;c<cl;c++){ |
155
|
|
|
var char = str.charAt(c); |
156
|
|
|
if (char.isOfCharRange(type)) { |
157
|
|
|
output.push(char); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
return output.join(""); |
161
|
|
|
}, |
162
|
|
View Code Duplication |
afterCharRange: function(type, inclusive = true, startAt = 0, forward = true, returnAll = false){ |
|
|
|
|
163
|
|
|
var text = this; |
164
|
|
|
if (text == null) { return returnAll ? text : ''; } |
|
|
|
|
165
|
|
|
if (!forward) { |
166
|
|
|
var idx = S.LastIndexOfCharRange(text, type); |
|
|
|
|
167
|
|
|
}else { |
168
|
|
|
idx = S.IndexOfCharRange(text, type, startAt); |
169
|
|
|
} |
170
|
|
|
if (idx == -1) { return returnAll ? text : ''; } |
171
|
|
|
if (!inclusive) { |
172
|
|
|
idx++; |
173
|
|
|
} |
174
|
|
|
return text.substr(idx); |
175
|
|
|
}, |
176
|
|
View Code Duplication |
beforeCharRange: function(type, inclusive = true, startAt = 0, forward = true, returnAll = false){ |
|
|
|
|
177
|
|
|
var text = this; |
178
|
|
|
if (text == null) { return returnAll ? text : ''; } |
|
|
|
|
179
|
|
|
if (forward) { |
180
|
|
|
var idx = S.IndexOfCharRange(text, type, startAt); |
|
|
|
|
181
|
|
|
}else { |
182
|
|
|
idx = S.LastIndexOfCharRange(text, type); |
183
|
|
|
} |
184
|
|
|
if (idx == -1) { return returnAll ? text : ''; } |
185
|
|
|
if (!inclusive) { |
186
|
|
|
idx--; |
187
|
|
|
} |
188
|
|
|
return text.substr(0, idx); |
189
|
|
|
}, |
190
|
|
|
|
191
|
|
|
// digits |
192
|
|
|
removeDigits: function(){ |
193
|
|
|
var str = this; |
194
|
|
|
|
195
|
|
|
// exit quickly if blank string |
196
|
|
|
if (str == null || str.length == 0) { |
|
|
|
|
197
|
|
|
return ""; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// extract all chars that are not digits |
201
|
|
|
var output = []; |
202
|
|
|
for (var c = 0, cl = str.length;c<cl;c++){ |
203
|
|
|
var char = str.charAt(c); |
204
|
|
|
if (!S.IsDigit(char)) { |
|
|
|
|
205
|
|
|
output.push(char); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
return output.join(""); |
209
|
|
|
}, |
210
|
|
|
|
211
|
|
|
none:null |
212
|
|
|
}; |
213
|
|
|
|
214
|
|
|
// register funcs |
215
|
|
|
UB.registerFuncs(String.prototype, stringFuncs); |
|
|
|
|