2 // TODO: Remove from `core-js@4` since it's moved to entry points
3 require('../modules/es.regexp.exec');
4 var redefine = require('../internals/redefine');
5 var fails = require('../internals/fails');
6 var wellKnownSymbol = require('../internals/well-known-symbol');
7 var regexpExec = require('../internals/regexp-exec');
8 var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
10 var SPECIES = wellKnownSymbol('species');
12 var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
13 // #replace needs built-in support for named groups.
14 // #match works fine because it just return the exec results, even if it has
15 // a "grops" property.
17 re.exec = function () {
19 result.groups = { a: '7' };
22 return ''.replace(re, '$<a>') !== '7';
25 // IE <= 11 replaces $0 with the whole match, as if it was $&
26 // https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0
27 var REPLACE_KEEPS_$0 = (function () {
28 return 'a'.replace(/./, '$0') === '$0';
31 var REPLACE = wellKnownSymbol('replace');
32 // Safari <= 13.0.3(?) substitutes nth capture where n>m with an empty string
33 var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
35 return /./[REPLACE]('a', '$0') === '';
40 // Chrome 51 has a buggy "split" implementation when RegExp#exec !== nativeExec
41 // Weex JS has frozen built-in prototypes, so use try / catch wrapper
42 var SPLIT_WORKS_WITH_OVERWRITTEN_EXEC = !fails(function () {
44 var originalExec = re.exec;
45 re.exec = function () { return originalExec.apply(this, arguments); };
46 var result = 'ab'.split(re);
47 return result.length !== 2 || result[0] !== 'a' || result[1] !== 'b';
50 module.exports = function (KEY, length, exec, sham) {
51 var SYMBOL = wellKnownSymbol(KEY);
53 var DELEGATES_TO_SYMBOL = !fails(function () {
54 // String methods call symbol-named RegEp methods
56 O[SYMBOL] = function () { return 7; };
57 return ''[KEY](O) != 7;
60 var DELEGATES_TO_EXEC = DELEGATES_TO_SYMBOL && !fails(function () {
61 // Symbol-named RegExp methods call .exec
62 var execCalled = false;
65 if (KEY === 'split') {
66 // We can't use real regex here since it causes deoptimization
67 // and serious performance degradation in V8
68 // https://github.com/zloirock/core-js/issues/306
70 // RegExp[@@split] doesn't call the regex's exec method, but first creates
71 // a new one. We need to return the patched regex when creating the new one.
73 re.constructor[SPECIES] = function () { return re; };
75 re[SYMBOL] = /./[SYMBOL];
78 re.exec = function () { execCalled = true; return null; };
85 !DELEGATES_TO_SYMBOL ||
87 (KEY === 'replace' && !(
88 REPLACE_SUPPORTS_NAMED_GROUPS &&
90 !REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
92 (KEY === 'split' && !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC)
94 var nativeRegExpMethod = /./[SYMBOL];
95 var methods = exec(SYMBOL, ''[KEY], function (nativeMethod, regexp, str, arg2, forceStringMethod) {
96 if (regexp.exec === regexpExec) {
97 if (DELEGATES_TO_SYMBOL && !forceStringMethod) {
98 // The native String method already delegates to @@method (this
99 // polyfilled function), leasing to infinite recursion.
100 // We avoid it by directly calling the native @@method method.
101 return { done: true, value: nativeRegExpMethod.call(regexp, str, arg2) };
103 return { done: true, value: nativeMethod.call(str, regexp, arg2) };
105 return { done: false };
107 REPLACE_KEEPS_$0: REPLACE_KEEPS_$0,
108 REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE: REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
110 var stringMethod = methods[0];
111 var regexMethod = methods[1];
113 redefine(String.prototype, KEY, stringMethod);
114 redefine(RegExp.prototype, SYMBOL, length == 2
115 // 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue)
116 // 21.2.5.11 RegExp.prototype[@@split](string, limit)
117 ? function (string, arg) { return regexMethod.call(string, this, arg); }
118 // 21.2.5.6 RegExp.prototype[@@match](string)
119 // 21.2.5.9 RegExp.prototype[@@search](string)
120 : function (string) { return regexMethod.call(string, this); }
124 if (sham) createNonEnumerableProperty(RegExp.prototype[SYMBOL], 'sham', true);