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