Pattern.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Generated by CoffeeScript 1.12.4
  2. var Pattern;
  3. Pattern = (function() {
  4. Pattern.prototype.regex = null;
  5. Pattern.prototype.rawRegex = null;
  6. Pattern.prototype.cleanedRegex = null;
  7. Pattern.prototype.mapping = null;
  8. function Pattern(rawRegex, modifiers) {
  9. var _char, capturingBracketNumber, cleanedRegex, i, len, mapping, name, part, subChar;
  10. if (modifiers == null) {
  11. modifiers = '';
  12. }
  13. cleanedRegex = '';
  14. len = rawRegex.length;
  15. mapping = null;
  16. capturingBracketNumber = 0;
  17. i = 0;
  18. while (i < len) {
  19. _char = rawRegex.charAt(i);
  20. if (_char === '\\') {
  21. cleanedRegex += rawRegex.slice(i, +(i + 1) + 1 || 9e9);
  22. i++;
  23. } else if (_char === '(') {
  24. if (i < len - 2) {
  25. part = rawRegex.slice(i, +(i + 2) + 1 || 9e9);
  26. if (part === '(?:') {
  27. i += 2;
  28. cleanedRegex += part;
  29. } else if (part === '(?<') {
  30. capturingBracketNumber++;
  31. i += 2;
  32. name = '';
  33. while (i + 1 < len) {
  34. subChar = rawRegex.charAt(i + 1);
  35. if (subChar === '>') {
  36. cleanedRegex += '(';
  37. i++;
  38. if (name.length > 0) {
  39. if (mapping == null) {
  40. mapping = {};
  41. }
  42. mapping[name] = capturingBracketNumber;
  43. }
  44. break;
  45. } else {
  46. name += subChar;
  47. }
  48. i++;
  49. }
  50. } else {
  51. cleanedRegex += _char;
  52. capturingBracketNumber++;
  53. }
  54. } else {
  55. cleanedRegex += _char;
  56. }
  57. } else {
  58. cleanedRegex += _char;
  59. }
  60. i++;
  61. }
  62. this.rawRegex = rawRegex;
  63. this.cleanedRegex = cleanedRegex;
  64. this.regex = new RegExp(this.cleanedRegex, 'g' + modifiers.replace('g', ''));
  65. this.mapping = mapping;
  66. }
  67. Pattern.prototype.exec = function(str) {
  68. var index, matches, name, ref;
  69. this.regex.lastIndex = 0;
  70. matches = this.regex.exec(str);
  71. if (matches == null) {
  72. return null;
  73. }
  74. if (this.mapping != null) {
  75. ref = this.mapping;
  76. for (name in ref) {
  77. index = ref[name];
  78. matches[name] = matches[index];
  79. }
  80. }
  81. return matches;
  82. };
  83. Pattern.prototype.test = function(str) {
  84. this.regex.lastIndex = 0;
  85. return this.regex.test(str);
  86. };
  87. Pattern.prototype.replace = function(str, replacement) {
  88. this.regex.lastIndex = 0;
  89. return str.replace(this.regex, replacement);
  90. };
  91. Pattern.prototype.replaceAll = function(str, replacement, limit) {
  92. var count;
  93. if (limit == null) {
  94. limit = 0;
  95. }
  96. this.regex.lastIndex = 0;
  97. count = 0;
  98. while (this.regex.test(str) && (limit === 0 || count < limit)) {
  99. this.regex.lastIndex = 0;
  100. str = str.replace(this.regex, replacement);
  101. count++;
  102. }
  103. return [str, count];
  104. };
  105. return Pattern;
  106. })();
  107. module.exports = Pattern;