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 var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined;
26 var PATCH = UPDATES_LAST_INDEX_WRONG || NPCG_INCLUDED || UNSUPPORTED_Y;
29 patchedExec = function exec(str) {
31 var lastIndex, reCopy, match, i;
32 var sticky = UNSUPPORTED_Y && re.sticky;
33 var flags = regexpFlags.call(re);
34 var source = re.source;
39 flags = flags.replace('y', '');
40 if (flags.indexOf('g') === -1) {
44 strCopy = String(str).slice(re.lastIndex);
45 // Support anchored sticky behavior.
46 if (re.lastIndex > 0 && (!re.multiline || re.multiline && str[re.lastIndex - 1] !== '\n')) {
47 source = '(?: ' + source + ')';
48 strCopy = ' ' + strCopy;
51 // ^(? + rx + ) is needed, in combination with some str slicing, to
52 // simulate the 'y' flag.
53 reCopy = new RegExp('^(?:' + source + ')', flags);
57 reCopy = new RegExp('^' + source + '$(?!\\s)', flags);
59 if (UPDATES_LAST_INDEX_WRONG) lastIndex = re.lastIndex;
61 match = nativeExec.call(sticky ? reCopy : re, strCopy);
65 match.input = match.input.slice(charsAdded);
66 match[0] = match[0].slice(charsAdded);
67 match.index = re.lastIndex;
68 re.lastIndex += match[0].length;
69 } else re.lastIndex = 0;
70 } else if (UPDATES_LAST_INDEX_WRONG && match) {
71 re.lastIndex = re.global ? match.index + match[0].length : lastIndex;
73 if (NPCG_INCLUDED && match && match.length > 1) {
74 // Fix browsers whose `exec` methods don't consistently return `undefined`
75 // for NPCG, like IE8. NOTE: This doesn' work for /(.?)?/
76 nativeReplace.call(match[0], reCopy, function () {
77 for (i = 1; i < arguments.length - 2; i++) {
78 if (arguments[i] === undefined) match[i] = undefined;
87 module.exports = patchedExec;