1 | 'use strict';
|
2 | var regexpFlags = require('./regexp-flags');
|
3 |
|
4 | var nativeExec = RegExp.prototype.exec;
|
5 |
|
6 |
|
7 |
|
8 | var nativeReplace = String.prototype.replace;
|
9 |
|
10 | var patchedExec = nativeExec;
|
11 |
|
12 | var UPDATES_LAST_INDEX_WRONG = (function () {
|
13 | var re1 = /a/;
|
14 | var re2 = /b*/g;
|
15 | nativeExec.call(re1, 'a');
|
16 | nativeExec.call(re2, 'a');
|
17 | return re1.lastIndex !== 0 || re2.lastIndex !== 0;
|
18 | })();
|
19 |
|
20 |
|
21 | var NPCG_INCLUDED = /()??/.exec('')[1] !== undefined;
|
22 |
|
23 | var PATCH = UPDATES_LAST_INDEX_WRONG || NPCG_INCLUDED;
|
24 |
|
25 | if (PATCH) {
|
26 | patchedExec = function exec(str) {
|
27 | var re = this;
|
28 | var lastIndex, reCopy, match, i;
|
29 |
|
30 | if (NPCG_INCLUDED) {
|
31 | reCopy = new RegExp('^' + re.source + '$(?!\\s)', regexpFlags.call(re));
|
32 | }
|
33 | if (UPDATES_LAST_INDEX_WRONG) lastIndex = re.lastIndex;
|
34 |
|
35 | match = nativeExec.call(re, str);
|
36 |
|
37 | if (UPDATES_LAST_INDEX_WRONG && match) {
|
38 | re.lastIndex = re.global ? match.index + match[0].length : lastIndex;
|
39 | }
|
40 | if (NPCG_INCLUDED && match && match.length > 1) {
|
41 |
|
42 |
|
43 | nativeReplace.call(match[0], reCopy, function () {
|
44 | for (i = 1; i < arguments.length - 2; i++) {
|
45 | if (arguments[i] === undefined) match[i] = undefined;
|
46 | }
|
47 | });
|
48 | }
|
49 |
|
50 | return match;
|
51 | };
|
52 | }
|
53 |
|
54 | module.exports = patchedExec;
|