2010-11-15 08:39:52 +01:00
|
|
|
/*
|
2011-11-08 17:40:44 +01:00
|
|
|
Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
|
2010-11-15 08:39:52 +01:00
|
|
|
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
|
|
|
see: http://dojotoolkit.org/license for details
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-03-04 17:02:28 +01:00
|
|
|
if(!dojo._hasResource["dojo.regexp"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
|
dojo._hasResource["dojo.regexp"] = true;
|
2010-11-15 08:39:52 +01:00
|
|
|
dojo.provide("dojo.regexp");
|
2011-03-04 17:02:28 +01:00
|
|
|
|
2011-11-08 17:40:44 +01:00
|
|
|
dojo.getObject("regexp", true, dojo);
|
|
|
|
|
2011-03-04 17:02:28 +01:00
|
|
|
/*=====
|
|
|
|
dojo.regexp = {
|
|
|
|
// summary: Regular expressions and Builder resources
|
2010-11-15 08:39:52 +01:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
=====*/
|
|
|
|
|
|
|
|
dojo.regexp.escapeString = function(/*String*/str, /*String?*/except){
|
|
|
|
// summary:
|
|
|
|
// Adds escape sequences for special characters in regular expressions
|
|
|
|
// except:
|
|
|
|
// a String with special characters to be left unescaped
|
|
|
|
|
|
|
|
return str.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, function(ch){
|
|
|
|
if(except && except.indexOf(ch) != -1){
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
return "\\" + ch;
|
|
|
|
}); // String
|
2011-11-08 17:40:44 +01:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
|
|
|
dojo.regexp.buildGroupRE = function(/*Object|Array*/arr, /*Function*/re, /*Boolean?*/nonCapture){
|
|
|
|
// summary:
|
|
|
|
// Builds a regular expression that groups subexpressions
|
|
|
|
// description:
|
|
|
|
// A utility function used by some of the RE generators. The
|
|
|
|
// subexpressions are constructed by the function, re, in the second
|
|
|
|
// parameter. re builds one subexpression for each elem in the array
|
|
|
|
// a, in the first parameter. Returns a string for a regular
|
|
|
|
// expression that groups all the subexpressions.
|
|
|
|
// arr:
|
|
|
|
// A single value or an array of values.
|
|
|
|
// re:
|
|
|
|
// A function. Takes one parameter and converts it to a regular
|
2011-11-08 17:40:44 +01:00
|
|
|
// expression.
|
2011-03-04 17:02:28 +01:00
|
|
|
// nonCapture:
|
|
|
|
// If true, uses non-capturing match, otherwise matches are retained
|
|
|
|
// by regular expression. Defaults to false
|
|
|
|
|
|
|
|
// case 1: a is a single value.
|
|
|
|
if(!(arr instanceof Array)){
|
|
|
|
return re(arr); // String
|
|
|
|
}
|
|
|
|
|
|
|
|
// case 2: a is an array
|
|
|
|
var b = [];
|
|
|
|
for(var i = 0; i < arr.length; i++){
|
|
|
|
// convert each elem to a RE
|
|
|
|
b.push(re(arr[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// join the REs as alternatives in a RE group.
|
|
|
|
return dojo.regexp.group(b.join("|"), nonCapture); // String
|
2011-11-08 17:40:44 +01:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
|
|
|
dojo.regexp.group = function(/*String*/expression, /*Boolean?*/nonCapture){
|
|
|
|
// summary:
|
|
|
|
// adds group match to expression
|
|
|
|
// nonCapture:
|
|
|
|
// If true, uses non-capturing match, otherwise matches are retained
|
2011-11-08 17:40:44 +01:00
|
|
|
// by regular expression.
|
2011-03-04 17:02:28 +01:00
|
|
|
return "(" + (nonCapture ? "?:":"") + expression + ")"; // String
|
2011-11-08 17:40:44 +01:00
|
|
|
};
|
2011-03-04 17:02:28 +01:00
|
|
|
|
2010-11-15 08:39:52 +01:00
|
|
|
}
|