1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
dojo.provide("dojo.regexp");
|
|
|
12 |
dojo.evalObjPath("dojo.regexp.us", true);
|
|
|
13 |
dojo.regexp.tld = function (flags) {
|
|
|
14 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
15 |
if (typeof flags.allowCC != "boolean") {
|
|
|
16 |
flags.allowCC = true;
|
|
|
17 |
}
|
|
|
18 |
if (typeof flags.allowInfra != "boolean") {
|
|
|
19 |
flags.allowInfra = true;
|
|
|
20 |
}
|
|
|
21 |
if (typeof flags.allowGeneric != "boolean") {
|
|
|
22 |
flags.allowGeneric = true;
|
|
|
23 |
}
|
|
|
24 |
var infraRE = "arpa";
|
|
|
25 |
var genericRE = "aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|xxx|jobs|mobi|post";
|
|
|
26 |
var ccRE = "ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|" + "bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|" + "ec|ee|eg|er|eu|es|et|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|" + "gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|" + "la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|" + "my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|" + "re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sk|sl|sm|sn|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|" + "tn|to|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw";
|
|
|
27 |
var a = [];
|
|
|
28 |
if (flags.allowInfra) {
|
|
|
29 |
a.push(infraRE);
|
|
|
30 |
}
|
|
|
31 |
if (flags.allowGeneric) {
|
|
|
32 |
a.push(genericRE);
|
|
|
33 |
}
|
|
|
34 |
if (flags.allowCC) {
|
|
|
35 |
a.push(ccRE);
|
|
|
36 |
}
|
|
|
37 |
var tldRE = "";
|
|
|
38 |
if (a.length > 0) {
|
|
|
39 |
tldRE = "(" + a.join("|") + ")";
|
|
|
40 |
}
|
|
|
41 |
return tldRE;
|
|
|
42 |
};
|
|
|
43 |
dojo.regexp.ipAddress = function (flags) {
|
|
|
44 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
45 |
if (typeof flags.allowDottedDecimal != "boolean") {
|
|
|
46 |
flags.allowDottedDecimal = true;
|
|
|
47 |
}
|
|
|
48 |
if (typeof flags.allowDottedHex != "boolean") {
|
|
|
49 |
flags.allowDottedHex = true;
|
|
|
50 |
}
|
|
|
51 |
if (typeof flags.allowDottedOctal != "boolean") {
|
|
|
52 |
flags.allowDottedOctal = true;
|
|
|
53 |
}
|
|
|
54 |
if (typeof flags.allowDecimal != "boolean") {
|
|
|
55 |
flags.allowDecimal = true;
|
|
|
56 |
}
|
|
|
57 |
if (typeof flags.allowHex != "boolean") {
|
|
|
58 |
flags.allowHex = true;
|
|
|
59 |
}
|
|
|
60 |
if (typeof flags.allowIPv6 != "boolean") {
|
|
|
61 |
flags.allowIPv6 = true;
|
|
|
62 |
}
|
|
|
63 |
if (typeof flags.allowHybrid != "boolean") {
|
|
|
64 |
flags.allowHybrid = true;
|
|
|
65 |
}
|
|
|
66 |
var dottedDecimalRE = "((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
|
|
|
67 |
var dottedHexRE = "(0[xX]0*[\\da-fA-F]?[\\da-fA-F]\\.){3}0[xX]0*[\\da-fA-F]?[\\da-fA-F]";
|
|
|
68 |
var dottedOctalRE = "(0+[0-3][0-7][0-7]\\.){3}0+[0-3][0-7][0-7]";
|
|
|
69 |
var decimalRE = "(0|[1-9]\\d{0,8}|[1-3]\\d{9}|4[01]\\d{8}|42[0-8]\\d{7}|429[0-3]\\d{6}|" + "4294[0-8]\\d{5}|42949[0-5]\\d{4}|429496[0-6]\\d{3}|4294967[01]\\d{2}|42949672[0-8]\\d|429496729[0-5])";
|
|
|
70 |
var hexRE = "0[xX]0*[\\da-fA-F]{1,8}";
|
|
|
71 |
var ipv6RE = "([\\da-fA-F]{1,4}\\:){7}[\\da-fA-F]{1,4}";
|
|
|
72 |
var hybridRE = "([\\da-fA-F]{1,4}\\:){6}" + "((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
|
|
|
73 |
var a = [];
|
|
|
74 |
if (flags.allowDottedDecimal) {
|
|
|
75 |
a.push(dottedDecimalRE);
|
|
|
76 |
}
|
|
|
77 |
if (flags.allowDottedHex) {
|
|
|
78 |
a.push(dottedHexRE);
|
|
|
79 |
}
|
|
|
80 |
if (flags.allowDottedOctal) {
|
|
|
81 |
a.push(dottedOctalRE);
|
|
|
82 |
}
|
|
|
83 |
if (flags.allowDecimal) {
|
|
|
84 |
a.push(decimalRE);
|
|
|
85 |
}
|
|
|
86 |
if (flags.allowHex) {
|
|
|
87 |
a.push(hexRE);
|
|
|
88 |
}
|
|
|
89 |
if (flags.allowIPv6) {
|
|
|
90 |
a.push(ipv6RE);
|
|
|
91 |
}
|
|
|
92 |
if (flags.allowHybrid) {
|
|
|
93 |
a.push(hybridRE);
|
|
|
94 |
}
|
|
|
95 |
var ipAddressRE = "";
|
|
|
96 |
if (a.length > 0) {
|
|
|
97 |
ipAddressRE = "(" + a.join("|") + ")";
|
|
|
98 |
}
|
|
|
99 |
return ipAddressRE;
|
|
|
100 |
};
|
|
|
101 |
dojo.regexp.host = function (flags) {
|
|
|
102 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
103 |
if (typeof flags.allowIP != "boolean") {
|
|
|
104 |
flags.allowIP = true;
|
|
|
105 |
}
|
|
|
106 |
if (typeof flags.allowLocal != "boolean") {
|
|
|
107 |
flags.allowLocal = false;
|
|
|
108 |
}
|
|
|
109 |
if (typeof flags.allowPort != "boolean") {
|
|
|
110 |
flags.allowPort = true;
|
|
|
111 |
}
|
|
|
112 |
var domainNameRE = "([0-9a-zA-Z]([-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?\\.)+" + dojo.regexp.tld(flags);
|
|
|
113 |
var portRE = (flags.allowPort) ? "(\\:" + dojo.regexp.integer({signed:false}) + ")?" : "";
|
|
|
114 |
var hostNameRE = domainNameRE;
|
|
|
115 |
if (flags.allowIP) {
|
|
|
116 |
hostNameRE += "|" + dojo.regexp.ipAddress(flags);
|
|
|
117 |
}
|
|
|
118 |
if (flags.allowLocal) {
|
|
|
119 |
hostNameRE += "|localhost";
|
|
|
120 |
}
|
|
|
121 |
return "(" + hostNameRE + ")" + portRE;
|
|
|
122 |
};
|
|
|
123 |
dojo.regexp.url = function (flags) {
|
|
|
124 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
125 |
if (typeof flags.scheme == "undefined") {
|
|
|
126 |
flags.scheme = [true, false];
|
|
|
127 |
}
|
|
|
128 |
var protocolRE = dojo.regexp.buildGroupRE(flags.scheme, function (q) {
|
|
|
129 |
if (q) {
|
|
|
130 |
return "(https?|ftps?)\\://";
|
|
|
131 |
}
|
|
|
132 |
return "";
|
|
|
133 |
});
|
|
|
134 |
var pathRE = "(/([^?#\\s/]+/)*)?([^?#\\s/]+(\\?[^?#\\s/]*)?(#[A-Za-z][\\w.:-]*)?)?";
|
|
|
135 |
return protocolRE + dojo.regexp.host(flags) + pathRE;
|
|
|
136 |
};
|
|
|
137 |
dojo.regexp.emailAddress = function (flags) {
|
|
|
138 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
139 |
if (typeof flags.allowCruft != "boolean") {
|
|
|
140 |
flags.allowCruft = false;
|
|
|
141 |
}
|
|
|
142 |
flags.allowPort = false;
|
|
|
143 |
var usernameRE = "([\\da-z]+[-._+&'])*[\\da-z]+";
|
|
|
144 |
var emailAddressRE = usernameRE + "@" + dojo.regexp.host(flags);
|
|
|
145 |
if (flags.allowCruft) {
|
|
|
146 |
emailAddressRE = "<?(mailto\\:)?" + emailAddressRE + ">?";
|
|
|
147 |
}
|
|
|
148 |
return emailAddressRE;
|
|
|
149 |
};
|
|
|
150 |
dojo.regexp.emailAddressList = function (flags) {
|
|
|
151 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
152 |
if (typeof flags.listSeparator != "string") {
|
|
|
153 |
flags.listSeparator = "\\s;,";
|
|
|
154 |
}
|
|
|
155 |
var emailAddressRE = dojo.regexp.emailAddress(flags);
|
|
|
156 |
var emailAddressListRE = "(" + emailAddressRE + "\\s*[" + flags.listSeparator + "]\\s*)*" + emailAddressRE + "\\s*[" + flags.listSeparator + "]?\\s*";
|
|
|
157 |
return emailAddressListRE;
|
|
|
158 |
};
|
|
|
159 |
dojo.regexp.integer = function (flags) {
|
|
|
160 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
161 |
if (typeof flags.signed == "undefined") {
|
|
|
162 |
flags.signed = [true, false];
|
|
|
163 |
}
|
|
|
164 |
if (typeof flags.separator == "undefined") {
|
|
|
165 |
flags.separator = "";
|
|
|
166 |
} else {
|
|
|
167 |
if (typeof flags.groupSize == "undefined") {
|
|
|
168 |
flags.groupSize = 3;
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
var signRE = dojo.regexp.buildGroupRE(flags.signed, function (q) {
|
|
|
172 |
return q ? "[-+]" : "";
|
|
|
173 |
});
|
|
|
174 |
var numberRE = dojo.regexp.buildGroupRE(flags.separator, function (sep) {
|
|
|
175 |
if (sep == "") {
|
|
|
176 |
return "(0|[1-9]\\d*)";
|
|
|
177 |
}
|
|
|
178 |
var grp = flags.groupSize, grp2 = flags.groupSize2;
|
|
|
179 |
if (typeof grp2 != "undefined") {
|
|
|
180 |
var grp2RE = "(0|[1-9]\\d{0," + (grp2 - 1) + "}([" + sep + "]\\d{" + grp2 + "})*[" + sep + "]\\d{" + grp + "})";
|
|
|
181 |
return ((grp - grp2) > 0) ? "(" + grp2RE + "|(0|[1-9]\\d{0," + (grp - 1) + "}))" : grp2RE;
|
|
|
182 |
}
|
|
|
183 |
return "(0|[1-9]\\d{0," + (grp - 1) + "}([" + sep + "]\\d{" + grp + "})*)";
|
|
|
184 |
});
|
|
|
185 |
return signRE + numberRE;
|
|
|
186 |
};
|
|
|
187 |
dojo.regexp.realNumber = function (flags) {
|
|
|
188 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
189 |
if (typeof flags.places != "number") {
|
|
|
190 |
flags.places = Infinity;
|
|
|
191 |
}
|
|
|
192 |
if (typeof flags.decimal != "string") {
|
|
|
193 |
flags.decimal = ".";
|
|
|
194 |
}
|
|
|
195 |
if (typeof flags.fractional == "undefined") {
|
|
|
196 |
flags.fractional = [true, false];
|
|
|
197 |
}
|
|
|
198 |
if (typeof flags.exponent == "undefined") {
|
|
|
199 |
flags.exponent = [true, false];
|
|
|
200 |
}
|
|
|
201 |
if (typeof flags.eSigned == "undefined") {
|
|
|
202 |
flags.eSigned = [true, false];
|
|
|
203 |
}
|
|
|
204 |
var integerRE = dojo.regexp.integer(flags);
|
|
|
205 |
var decimalRE = dojo.regexp.buildGroupRE(flags.fractional, function (q) {
|
|
|
206 |
var re = "";
|
|
|
207 |
if (q && (flags.places > 0)) {
|
|
|
208 |
re = "\\" + flags.decimal;
|
|
|
209 |
if (flags.places == Infinity) {
|
|
|
210 |
re = "(" + re + "\\d+)?";
|
|
|
211 |
} else {
|
|
|
212 |
re = re + "\\d{" + flags.places + "}";
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
return re;
|
|
|
216 |
});
|
|
|
217 |
var exponentRE = dojo.regexp.buildGroupRE(flags.exponent, function (q) {
|
|
|
218 |
if (q) {
|
|
|
219 |
return "([eE]" + dojo.regexp.integer({signed:flags.eSigned}) + ")";
|
|
|
220 |
}
|
|
|
221 |
return "";
|
|
|
222 |
});
|
|
|
223 |
return integerRE + decimalRE + exponentRE;
|
|
|
224 |
};
|
|
|
225 |
dojo.regexp.currency = function (flags) {
|
|
|
226 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
227 |
if (typeof flags.signed == "undefined") {
|
|
|
228 |
flags.signed = [true, false];
|
|
|
229 |
}
|
|
|
230 |
if (typeof flags.symbol == "undefined") {
|
|
|
231 |
flags.symbol = "$";
|
|
|
232 |
}
|
|
|
233 |
if (typeof flags.placement != "string") {
|
|
|
234 |
flags.placement = "before";
|
|
|
235 |
}
|
|
|
236 |
if (typeof flags.signPlacement != "string") {
|
|
|
237 |
flags.signPlacement = "before";
|
|
|
238 |
}
|
|
|
239 |
if (typeof flags.separator == "undefined") {
|
|
|
240 |
flags.separator = ",";
|
|
|
241 |
}
|
|
|
242 |
if (typeof flags.fractional == "undefined" && typeof flags.cents != "undefined") {
|
|
|
243 |
dojo.deprecated("dojo.regexp.currency: flags.cents", "use flags.fractional instead", "0.5");
|
|
|
244 |
flags.fractional = flags.cents;
|
|
|
245 |
}
|
|
|
246 |
if (typeof flags.decimal != "string") {
|
|
|
247 |
flags.decimal = ".";
|
|
|
248 |
}
|
|
|
249 |
var signRE = dojo.regexp.buildGroupRE(flags.signed, function (q) {
|
|
|
250 |
if (q) {
|
|
|
251 |
return "[-+]";
|
|
|
252 |
}
|
|
|
253 |
return "";
|
|
|
254 |
});
|
|
|
255 |
var symbolRE = dojo.regexp.buildGroupRE(flags.symbol, function (symbol) {
|
|
|
256 |
return "\\s?" + symbol.replace(/([.$?*!=:|\\\/^])/g, "\\$1") + "\\s?";
|
|
|
257 |
});
|
|
|
258 |
switch (flags.signPlacement) {
|
|
|
259 |
case "before":
|
|
|
260 |
symbolRE = signRE + symbolRE;
|
|
|
261 |
break;
|
|
|
262 |
case "after":
|
|
|
263 |
symbolRE = symbolRE + signRE;
|
|
|
264 |
break;
|
|
|
265 |
}
|
|
|
266 |
var flagsCopy = flags;
|
|
|
267 |
flagsCopy.signed = false;
|
|
|
268 |
flagsCopy.exponent = false;
|
|
|
269 |
var numberRE = dojo.regexp.realNumber(flagsCopy);
|
|
|
270 |
var currencyRE;
|
|
|
271 |
switch (flags.placement) {
|
|
|
272 |
case "before":
|
|
|
273 |
currencyRE = symbolRE + numberRE;
|
|
|
274 |
break;
|
|
|
275 |
case "after":
|
|
|
276 |
currencyRE = numberRE + symbolRE;
|
|
|
277 |
break;
|
|
|
278 |
}
|
|
|
279 |
switch (flags.signPlacement) {
|
|
|
280 |
case "around":
|
|
|
281 |
currencyRE = "(" + currencyRE + "|" + "\\(" + currencyRE + "\\)" + ")";
|
|
|
282 |
break;
|
|
|
283 |
case "begin":
|
|
|
284 |
currencyRE = signRE + currencyRE;
|
|
|
285 |
break;
|
|
|
286 |
case "end":
|
|
|
287 |
currencyRE = currencyRE + signRE;
|
|
|
288 |
break;
|
|
|
289 |
}
|
|
|
290 |
return currencyRE;
|
|
|
291 |
};
|
|
|
292 |
dojo.regexp.us.state = function (flags) {
|
|
|
293 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
294 |
if (typeof flags.allowTerritories != "boolean") {
|
|
|
295 |
flags.allowTerritories = true;
|
|
|
296 |
}
|
|
|
297 |
if (typeof flags.allowMilitary != "boolean") {
|
|
|
298 |
flags.allowMilitary = true;
|
|
|
299 |
}
|
|
|
300 |
var statesRE = "AL|AK|AZ|AR|CA|CO|CT|DE|DC|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|" + "NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY";
|
|
|
301 |
var territoriesRE = "AS|FM|GU|MH|MP|PW|PR|VI";
|
|
|
302 |
var militaryRE = "AA|AE|AP";
|
|
|
303 |
if (flags.allowTerritories) {
|
|
|
304 |
statesRE += "|" + territoriesRE;
|
|
|
305 |
}
|
|
|
306 |
if (flags.allowMilitary) {
|
|
|
307 |
statesRE += "|" + militaryRE;
|
|
|
308 |
}
|
|
|
309 |
return "(" + statesRE + ")";
|
|
|
310 |
};
|
|
|
311 |
dojo.regexp.time = function (flags) {
|
|
|
312 |
dojo.deprecated("dojo.regexp.time", "Use dojo.date.parse instead", "0.5");
|
|
|
313 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
314 |
if (typeof flags.format == "undefined") {
|
|
|
315 |
flags.format = "h:mm:ss t";
|
|
|
316 |
}
|
|
|
317 |
if (typeof flags.amSymbol != "string") {
|
|
|
318 |
flags.amSymbol = "AM";
|
|
|
319 |
}
|
|
|
320 |
if (typeof flags.pmSymbol != "string") {
|
|
|
321 |
flags.pmSymbol = "PM";
|
|
|
322 |
}
|
|
|
323 |
var timeRE = function (format) {
|
|
|
324 |
format = format.replace(/([.$?*!=:|{}\(\)\[\]\\\/^])/g, "\\$1");
|
|
|
325 |
var amRE = flags.amSymbol.replace(/([.$?*!=:|{}\(\)\[\]\\\/^])/g, "\\$1");
|
|
|
326 |
var pmRE = flags.pmSymbol.replace(/([.$?*!=:|{}\(\)\[\]\\\/^])/g, "\\$1");
|
|
|
327 |
format = format.replace("hh", "(0[1-9]|1[0-2])");
|
|
|
328 |
format = format.replace("h", "([1-9]|1[0-2])");
|
|
|
329 |
format = format.replace("HH", "([01][0-9]|2[0-3])");
|
|
|
330 |
format = format.replace("H", "([0-9]|1[0-9]|2[0-3])");
|
|
|
331 |
format = format.replace("mm", "([0-5][0-9])");
|
|
|
332 |
format = format.replace("m", "([1-5][0-9]|[0-9])");
|
|
|
333 |
format = format.replace("ss", "([0-5][0-9])");
|
|
|
334 |
format = format.replace("s", "([1-5][0-9]|[0-9])");
|
|
|
335 |
format = format.replace("t", "\\s?(" + amRE + "|" + pmRE + ")\\s?");
|
|
|
336 |
return format;
|
|
|
337 |
};
|
|
|
338 |
return dojo.regexp.buildGroupRE(flags.format, timeRE);
|
|
|
339 |
};
|
|
|
340 |
dojo.regexp.numberFormat = function (flags) {
|
|
|
341 |
flags = (typeof flags == "object") ? flags : {};
|
|
|
342 |
if (typeof flags.format == "undefined") {
|
|
|
343 |
flags.format = "###-###-####";
|
|
|
344 |
}
|
|
|
345 |
var digitRE = function (format) {
|
|
|
346 |
format = format.replace(/([.$*!=:|{}\(\)\[\]\\\/^])/g, "\\$1");
|
|
|
347 |
format = format.replace(/\?/g, "\\d?");
|
|
|
348 |
format = format.replace(/#/g, "\\d");
|
|
|
349 |
return format;
|
|
|
350 |
};
|
|
|
351 |
return dojo.regexp.buildGroupRE(flags.format, digitRE);
|
|
|
352 |
};
|
|
|
353 |
dojo.regexp.buildGroupRE = function (a, re) {
|
|
|
354 |
if (!(a instanceof Array)) {
|
|
|
355 |
return re(a);
|
|
|
356 |
}
|
|
|
357 |
var b = [];
|
|
|
358 |
for (var i = 0; i < a.length; i++) {
|
|
|
359 |
b.push(re(a[i]));
|
|
|
360 |
}
|
|
|
361 |
return "(" + b.join("|") + ")";
|
|
|
362 |
};
|
|
|
363 |
|