[483] | 1 | define(["./_base/kernel", "./_base/lang"], function(dojo, lang){ |
---|
| 2 | |
---|
| 3 | // module: |
---|
| 4 | // dojo/regexp |
---|
| 5 | |
---|
| 6 | var regexp = { |
---|
| 7 | // summary: |
---|
| 8 | // Regular expressions and Builder resources |
---|
| 9 | }; |
---|
| 10 | lang.setObject("dojo.regexp", regexp); |
---|
| 11 | |
---|
| 12 | regexp.escapeString = function(/*String*/str, /*String?*/except){ |
---|
| 13 | // summary: |
---|
| 14 | // Adds escape sequences for special characters in regular expressions |
---|
| 15 | // except: |
---|
| 16 | // a String with special characters to be left unescaped |
---|
| 17 | |
---|
| 18 | return str.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, function(ch){ |
---|
| 19 | if(except && except.indexOf(ch) != -1){ |
---|
| 20 | return ch; |
---|
| 21 | } |
---|
| 22 | return "\\" + ch; |
---|
| 23 | }); // String |
---|
| 24 | }; |
---|
| 25 | |
---|
| 26 | regexp.buildGroupRE = function(/*Object|Array*/arr, /*Function*/re, /*Boolean?*/nonCapture){ |
---|
| 27 | // summary: |
---|
| 28 | // Builds a regular expression that groups subexpressions |
---|
| 29 | // description: |
---|
| 30 | // A utility function used by some of the RE generators. The |
---|
| 31 | // subexpressions are constructed by the function, re, in the second |
---|
| 32 | // parameter. re builds one subexpression for each elem in the array |
---|
| 33 | // a, in the first parameter. Returns a string for a regular |
---|
| 34 | // expression that groups all the subexpressions. |
---|
| 35 | // arr: |
---|
| 36 | // A single value or an array of values. |
---|
| 37 | // re: |
---|
| 38 | // A function. Takes one parameter and converts it to a regular |
---|
| 39 | // expression. |
---|
| 40 | // nonCapture: |
---|
| 41 | // If true, uses non-capturing match, otherwise matches are retained |
---|
| 42 | // by regular expression. Defaults to false |
---|
| 43 | |
---|
| 44 | // case 1: a is a single value. |
---|
| 45 | if(!(arr instanceof Array)){ |
---|
| 46 | return re(arr); // String |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | // case 2: a is an array |
---|
| 50 | var b = []; |
---|
| 51 | for(var i = 0; i < arr.length; i++){ |
---|
| 52 | // convert each elem to a RE |
---|
| 53 | b.push(re(arr[i])); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | // join the REs as alternatives in a RE group. |
---|
| 57 | return regexp.group(b.join("|"), nonCapture); // String |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | regexp.group = function(/*String*/expression, /*Boolean?*/nonCapture){ |
---|
| 61 | // summary: |
---|
| 62 | // adds group match to expression |
---|
| 63 | // nonCapture: |
---|
| 64 | // If true, uses non-capturing match, otherwise matches are retained |
---|
| 65 | // by regular expression. |
---|
| 66 | return "(" + (nonCapture ? "?:":"") + expression + ")"; // String |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | return regexp; |
---|
| 70 | }); |
---|