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 () {
43 // eslint-disable-next-line regexp/no-empty-group -- required for testing
45 var originalExec = re.exec;
46 re.exec = function () { return originalExec.apply(this, arguments); };
47 var result = 'ab'.split(re);
48 return result.length !== 2 || result[0] !== 'a' || result[1] !== 'b';
51 module.exports = function (KEY, length, exec, sham) {
52 var SYMBOL = wellKnownSymbol(KEY);
54 var DELEGATES_TO_SYMBOL = !fails(function () {
55 // String methods call symbol-named RegEp methods
57 O[SYMBOL] = function () { return 7; };
58 return ''[KEY](O) != 7;
61 var DELEGATES_TO_EXEC = DELEGATES_TO_SYMBOL && !fails(function () {
62 // Symbol-named RegExp methods call .exec
63 var execCalled = false;
66 if (KEY === 'split') {
67 // We can't use real regex here since it causes deoptimization
68 // and serious performance degradation in V8
69 // https://github.com/zloirock/core-js/issues/306
71 // RegExp[@@split] doesn't call the regex's exec method, but first creates
72 // a new one. We need to return the patched regex when creating the new one.
74 re.constructor[SPECIES] = function () { return re; };
76 re[SYMBOL] = /./[SYMBOL];
79 re.exec = function () { execCalled = true; return null; };
86 !DELEGATES_TO_SYMBOL ||
88 (KEY === 'replace' && !(
89 REPLACE_SUPPORTS_NAMED_GROUPS &&
91 !REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
93 (KEY === 'split' && !SPLIT_WORKS_WITH_OVERWRITTEN_EXEC)
95 var nativeRegExpMethod = /./[SYMBOL];
96 var methods = exec(SYMBOL, ''[KEY], function (nativeMethod, regexp, str, arg2, forceStringMethod) {
97 if (regexp.exec === regexpExec) {
98 if (DELEGATES_TO_SYMBOL && !forceStringMethod) {
99 // The native String method already delegates to @@method (this
100 // polyfilled function), leasing to infinite recursion.
101 // We avoid it by directly calling the native @@method method.
102 return { done: true, value: nativeRegExpMethod.call(regexp, str, arg2) };
104 return { done: true, value: nativeMethod.call(str, regexp, arg2) };
106 return { done: false };
108 REPLACE_KEEPS_$0: REPLACE_KEEPS_$0,
109 REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE: REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE
111 var stringMethod = methods[0];
112 var regexMethod = methods[1];
114 redefine(String.prototype, KEY, stringMethod);
115 redefine(RegExp.prototype, SYMBOL, length == 2
116 // 21.2.5.8 RegExp.prototype[@@replace](string, replaceValue)
117 // 21.2.5.11 RegExp.prototype[@@split](string, limit)
118 ? function (string, arg) { return regexMethod.call(string, this, arg); }
119 // 21.2.5.6 RegExp.prototype[@@match](string)
120 // 21.2.5.9 RegExp.prototype[@@search](string)
121 : function (string) { return regexMethod.call(string, this); }
125 if (sham) createNonEnumerableProperty(RegExp.prototype[SYMBOL], 'sham', true);