|
103 | 103 | const stringNormalizerMatcher = /[.?*+^$[\]\\(){}|]/g;
|
104 | 104 |
|
105 | 105 |
|
106 |
| - const suffixMatcher = /^(?:[+*?]|\{(?=((\d+)))\1,?(\d*)\})\??$/; |
| 106 | + const suffixMatcher = /^(?:[+*?]|\{(\d+)(?:,(\d*))?\})\??$/; |
107 | 107 |
|
108 | 108 |
|
109 | 109 | const tokenMatcher = /(\\.)|[-()|\[\]]((?=\?<?[=!]))?/g;
|
|
287 | 287 | return hasRefs
|
288 | 288 | });
|
289 | 289 |
|
| 290 | + const finalQuantifierMatcher = /\\\\|\\\{|(\{\d+(?:,\d*)?\}$)/g; |
| 291 | + const badQuantifierMatcher = /^\d*(?:,\d*)?\}/; |
| 292 | + |
| 293 | + function combinesAsQuantfier(x1, x2) { |
| 294 | + // first look for a bad start in x2 since this is fast |
| 295 | + const badStart = x2.source.match(badQuantifierMatcher); |
| 296 | + if (badStart == null) return false |
| 297 | + // Now scan the combination looking for a newly formed quantifier |
| 298 | + const haystack = x1.source + badStart[0]; |
| 299 | + let result; |
| 300 | + finalQuantifierMatcher.lastIndex = 0; |
| 301 | + while (result = finalQuantifierMatcher.exec(haystack)) { |
| 302 | + if (result[1] != null) return true |
| 303 | + } |
| 304 | + return false |
| 305 | + } |
| 306 | + |
| 307 | + |
290 | 308 | // When composing expressions into a sequence, regexps that have a top-level
|
291 | 309 | // choice operator must be wrapped in a non-capturing group. This function
|
292 | 310 | // detects whether the group is needed or not.
|
|
517 | 535 | && hasRefs(x1)
|
518 | 536 | && metadata.get(x1.key, 'hasFinalRef')
|
519 | 537 | && (/^\d/.test(x2.source))
|
| 538 | + || combinesAsQuantfier(x1, x2) |
520 | 539 | ? '(?:)'
|
521 | 540 | : ''
|
522 | 541 | ) + x2.source;
|
523 |
| - |
524 | 542 | return x2
|
525 | 543 | }
|
526 | 544 | }
|
|
721 | 739 | function suffix(quantifier, ...args) {
|
722 | 740 | if (typeof quantifier !== 'string') quantifier = '{' + String(quantifier) + '}';
|
723 | 741 | const match = quantifier.match(suffixMatcher);
|
724 |
| - if (!match || match[3] && Number(match[3]) < Number(match[2])) throw new SyntaxError("Invalid suffix '" + quantifier+ "'.") |
| 742 | + if (!match || match[2] && Number(match[2]) < Number(match[1])) throw new SyntaxError("Invalid suffix '" + quantifier+ "'.") |
725 | 743 | return args.length === 0
|
726 | 744 | ? _suffix.bind(null, quantifier)
|
727 | 745 | : _suffix(quantifier, ...args)
|
|
0 commit comments