source: Dev/branches/rest-dojo-ui/client/dojo/regexp.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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