regexp.js 2.1 KB

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