1 | 'use strict';
|
---|
2 | /* eslint-disable regexp/no-empty-capturing-group, regexp/no-empty-group, regexp/no-lazy-ends -- testing */
|
---|
3 | /* eslint-disable regexp/no-useless-quantifier -- testing */
|
---|
4 | var call = require('../internals/function-call');
|
---|
5 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
6 | var toString = require('../internals/to-string');
|
---|
7 | var regexpFlags = require('../internals/regexp-flags');
|
---|
8 | var stickyHelpers = require('../internals/regexp-sticky-helpers');
|
---|
9 | var shared = require('../internals/shared');
|
---|
10 | var create = require('../internals/object-create');
|
---|
11 | var getInternalState = require('../internals/internal-state').get;
|
---|
12 | var UNSUPPORTED_DOT_ALL = require('../internals/regexp-unsupported-dot-all');
|
---|
13 | var UNSUPPORTED_NCG = require('../internals/regexp-unsupported-ncg');
|
---|
14 |
|
---|
15 | var nativeReplace = shared('native-string-replace', String.prototype.replace);
|
---|
16 | var nativeExec = RegExp.prototype.exec;
|
---|
17 | var patchedExec = nativeExec;
|
---|
18 | var charAt = uncurryThis(''.charAt);
|
---|
19 | var indexOf = uncurryThis(''.indexOf);
|
---|
20 | var replace = uncurryThis(''.replace);
|
---|
21 | var stringSlice = uncurryThis(''.slice);
|
---|
22 |
|
---|
23 | var UPDATES_LAST_INDEX_WRONG = (function () {
|
---|
24 | var re1 = /a/;
|
---|
25 | var re2 = /b*/g;
|
---|
26 | call(nativeExec, re1, 'a');
|
---|
27 | call(nativeExec, re2, 'a');
|
---|
28 | return re1.lastIndex !== 0 || re2.lastIndex !== 0;
|
---|
29 | })();
|
---|
30 |
|
---|
31 | var UNSUPPORTED_Y = stickyHelpers.BROKEN_CARET;
|
---|
32 |
|
---|
33 | // nonparticipating capturing group, copied from es5-shim's String#split patch.
|
---|
34 | var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined;
|
---|
35 |
|
---|
36 | var PATCH = UPDATES_LAST_INDEX_WRONG || NPCG_INCLUDED || UNSUPPORTED_Y || UNSUPPORTED_DOT_ALL || UNSUPPORTED_NCG;
|
---|
37 |
|
---|
38 | if (PATCH) {
|
---|
39 | patchedExec = function exec(string) {
|
---|
40 | var re = this;
|
---|
41 | var state = getInternalState(re);
|
---|
42 | var str = toString(string);
|
---|
43 | var raw = state.raw;
|
---|
44 | var result, reCopy, lastIndex, match, i, object, group;
|
---|
45 |
|
---|
46 | if (raw) {
|
---|
47 | raw.lastIndex = re.lastIndex;
|
---|
48 | result = call(patchedExec, raw, str);
|
---|
49 | re.lastIndex = raw.lastIndex;
|
---|
50 | return result;
|
---|
51 | }
|
---|
52 |
|
---|
53 | var groups = state.groups;
|
---|
54 | var sticky = UNSUPPORTED_Y && re.sticky;
|
---|
55 | var flags = call(regexpFlags, re);
|
---|
56 | var source = re.source;
|
---|
57 | var charsAdded = 0;
|
---|
58 | var strCopy = str;
|
---|
59 |
|
---|
60 | if (sticky) {
|
---|
61 | flags = replace(flags, 'y', '');
|
---|
62 | if (indexOf(flags, 'g') === -1) {
|
---|
63 | flags += 'g';
|
---|
64 | }
|
---|
65 |
|
---|
66 | strCopy = stringSlice(str, re.lastIndex);
|
---|
67 | // Support anchored sticky behavior.
|
---|
68 | if (re.lastIndex > 0 && (!re.multiline || re.multiline && charAt(str, re.lastIndex - 1) !== '\n')) {
|
---|
69 | source = '(?: ' + source + ')';
|
---|
70 | strCopy = ' ' + strCopy;
|
---|
71 | charsAdded++;
|
---|
72 | }
|
---|
73 | // ^(? + rx + ) is needed, in combination with some str slicing, to
|
---|
74 | // simulate the 'y' flag.
|
---|
75 | reCopy = new RegExp('^(?:' + source + ')', flags);
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (NPCG_INCLUDED) {
|
---|
79 | reCopy = new RegExp('^' + source + '$(?!\\s)', flags);
|
---|
80 | }
|
---|
81 | if (UPDATES_LAST_INDEX_WRONG) lastIndex = re.lastIndex;
|
---|
82 |
|
---|
83 | match = call(nativeExec, sticky ? reCopy : re, strCopy);
|
---|
84 |
|
---|
85 | if (sticky) {
|
---|
86 | if (match) {
|
---|
87 | match.input = stringSlice(match.input, charsAdded);
|
---|
88 | match[0] = stringSlice(match[0], charsAdded);
|
---|
89 | match.index = re.lastIndex;
|
---|
90 | re.lastIndex += match[0].length;
|
---|
91 | } else re.lastIndex = 0;
|
---|
92 | } else if (UPDATES_LAST_INDEX_WRONG && match) {
|
---|
93 | re.lastIndex = re.global ? match.index + match[0].length : lastIndex;
|
---|
94 | }
|
---|
95 | if (NPCG_INCLUDED && match && match.length > 1) {
|
---|
96 | // Fix browsers whose `exec` methods don't consistently return `undefined`
|
---|
97 | // for NPCG, like IE8. NOTE: This doesn't work for /(.?)?/
|
---|
98 | call(nativeReplace, match[0], reCopy, function () {
|
---|
99 | for (i = 1; i < arguments.length - 2; i++) {
|
---|
100 | if (arguments[i] === undefined) match[i] = undefined;
|
---|
101 | }
|
---|
102 | });
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (match && groups) {
|
---|
106 | match.groups = object = create(null);
|
---|
107 | for (i = 0; i < groups.length; i++) {
|
---|
108 | group = groups[i];
|
---|
109 | object[group[0]] = match[group[1]];
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | return match;
|
---|
114 | };
|
---|
115 | }
|
---|
116 |
|
---|
117 | module.exports = patchedExec;
|
---|