2 var regexpFlags = require('./regexp-flags');
3 var stickyHelpers = require('./regexp-sticky-helpers');
5 var nativeExec = RegExp.prototype.exec;
6 // This always refers to the native implementation, because the
7 // String#replace polyfill uses ./fix-regexp-well-known-symbol-logic.js,
8 // which loads this file before patching the method.
9 var nativeReplace = String.prototype.replace;
11 var patchedExec = nativeExec;
13 var UPDATES_LAST_INDEX_WRONG = (function () {
16 nativeExec.call(re1, 'a');
17 nativeExec.call(re2, 'a');
18 return re1.lastIndex !== 0 || re2.lastIndex !== 0;
21 var UNSUPPORTED_Y = stickyHelpers.UNSUPPORTED_Y || stickyHelpers.BROKEN_CARET;
23 // nonparticipating capturing group, copied from es5-shim's String#split patch.
24 // eslint-disable-next-line regexp/no-assertion-capturing-group, regexp/no-empty-group -- required for testing
25 var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined;
27 var PATCH = UPDATES_LAST_INDEX_WRONG || NPCG_INCLUDED || UNSUPPORTED_Y;
30 patchedExec = function exec(str) {
32 var lastIndex, reCopy, match, i;
33 var sticky = UNSUPPORTED_Y && re.sticky;
34 var flags = regexpFlags.call(re);
35 var source = re.source;
40 flags = flags.replace('y', '');
41 if (flags.indexOf('g') === -1) {
45 strCopy = String(str).slice(re.lastIndex);
46 // Support anchored sticky behavior.
47 if (re.lastIndex > 0 && (!re.multiline || re.multiline && str[re.lastIndex - 1] !== '\n')) {
48 source = '(?: ' + source + ')';
49 strCopy = ' ' + strCopy;
52 // ^(? + rx + ) is needed, in combination with some str slicing, to
53 // simulate the 'y' flag.
54 reCopy = new RegExp('^(?:' + source + ')', flags);
58 reCopy = new RegExp('^' + source + '$(?!\\s)', flags);
60 if (UPDATES_LAST_INDEX_WRONG) lastIndex = re.lastIndex;
62 match = nativeExec.call(sticky ? reCopy : re, strCopy);
66 match.input = match.input.slice(charsAdded);
67 match[0] = match[0].slice(charsAdded);
68 match.index = re.lastIndex;
69 re.lastIndex += match[0].length;
70 } else re.lastIndex = 0;
71 } else if (UPDATES_LAST_INDEX_WRONG && match) {
72 re.lastIndex = re.global ? match.index + match[0].length : lastIndex;
74 if (NPCG_INCLUDED && match && match.length > 1) {
75 // Fix browsers whose `exec` methods don't consistently return `undefined`
76 // for NPCG, like IE8. NOTE: This doesn' work for /(.?)?/
77 nativeReplace.call(match[0], reCopy, function () {
78 for (i = 1; i < arguments.length - 2; i++) {
79 if (arguments[i] === undefined) match[i] = undefined;
88 module.exports = patchedExec;