]>
Commit | Line | Data |
---|---|---|
2f01fe57 | 1 | /* |
81bea17a | 2 | Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved. |
2f01fe57 AD |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
a089699c AD |
8 | if(!dojo._hasResource["dojo.regexp"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
9 | dojo._hasResource["dojo.regexp"] = true; | |
2f01fe57 | 10 | dojo.provide("dojo.regexp"); |
a089699c | 11 | |
81bea17a AD |
12 | dojo.getObject("regexp", true, dojo); |
13 | ||
a089699c AD |
14 | /*===== |
15 | dojo.regexp = { | |
16 | // summary: Regular expressions and Builder resources | |
2f01fe57 | 17 | }; |
a089699c AD |
18 | =====*/ |
19 | ||
20 | dojo.regexp.escapeString = function(/*String*/str, /*String?*/except){ | |
21 | // summary: | |
22 | // Adds escape sequences for special characters in regular expressions | |
23 | // except: | |
24 | // a String with special characters to be left unescaped | |
25 | ||
26 | return str.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, function(ch){ | |
27 | if(except && except.indexOf(ch) != -1){ | |
28 | return ch; | |
29 | } | |
30 | return "\\" + ch; | |
31 | }); // String | |
81bea17a | 32 | }; |
a089699c AD |
33 | |
34 | dojo.regexp.buildGroupRE = function(/*Object|Array*/arr, /*Function*/re, /*Boolean?*/nonCapture){ | |
35 | // summary: | |
36 | // Builds a regular expression that groups subexpressions | |
37 | // description: | |
38 | // A utility function used by some of the RE generators. The | |
39 | // subexpressions are constructed by the function, re, in the second | |
40 | // parameter. re builds one subexpression for each elem in the array | |
41 | // a, in the first parameter. Returns a string for a regular | |
42 | // expression that groups all the subexpressions. | |
43 | // arr: | |
44 | // A single value or an array of values. | |
45 | // re: | |
46 | // A function. Takes one parameter and converts it to a regular | |
81bea17a | 47 | // expression. |
a089699c AD |
48 | // nonCapture: |
49 | // If true, uses non-capturing match, otherwise matches are retained | |
50 | // by regular expression. Defaults to false | |
51 | ||
52 | // case 1: a is a single value. | |
53 | if(!(arr instanceof Array)){ | |
54 | return re(arr); // String | |
55 | } | |
56 | ||
57 | // case 2: a is an array | |
58 | var b = []; | |
59 | for(var i = 0; i < arr.length; i++){ | |
60 | // convert each elem to a RE | |
61 | b.push(re(arr[i])); | |
62 | } | |
63 | ||
64 | // join the REs as alternatives in a RE group. | |
65 | return dojo.regexp.group(b.join("|"), nonCapture); // String | |
81bea17a | 66 | }; |
a089699c AD |
67 | |
68 | dojo.regexp.group = function(/*String*/expression, /*Boolean?*/nonCapture){ | |
69 | // summary: | |
70 | // adds group match to expression | |
71 | // nonCapture: | |
72 | // If true, uses non-capturing match, otherwise matches are retained | |
81bea17a | 73 | // by regular expression. |
a089699c | 74 | return "(" + (nonCapture ? "?:":"") + expression + ")"; // String |
81bea17a | 75 | }; |
a089699c | 76 | |
2f01fe57 | 77 | } |