2 var apply = require('../internals/function-apply');
3 var call = require('../internals/function-call');
4 var uncurryThis = require('../internals/function-uncurry-this');
5 var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
6 var fails = require('../internals/fails');
7 var anObject = require('../internals/an-object');
8 var isCallable = require('../internals/is-callable');
9 var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
10 var toLength = require('../internals/to-length');
11 var toString = require('../internals/to-string');
12 var requireObjectCoercible = require('../internals/require-object-coercible');
13 var advanceStringIndex = require('../internals/advance-string-index');
14 var getMethod = require('../internals/get-method');
15 var getSubstitution = require('../internals/get-substitution');
16 var regExpExec = require('../internals/regexp-exec-abstract');
17 var wellKnownSymbol = require('../internals/well-known-symbol');
19 var REPLACE = wellKnownSymbol('replace');
22 var concat = uncurryThis([].concat);
23 var push = uncurryThis([].push);
24 var stringIndexOf = uncurryThis(''.indexOf);
25 var stringSlice = uncurryThis(''.slice);
27 var maybeToString = function (it) {
28 return it === undefined ? it : String(it);
31 // IE <= 11 replaces $0 with the whole match, as if it was $&
32 // https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0
33 var REPLACE_KEEPS_$0 = (function () {
34 // eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
35 return 'a'.replace(/./, '$0') === '$0';
38 // Safari <= 13.0.3(?) substitutes nth capture where n>m with an empty string
39 var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
41 return /./[REPLACE]('a', '$0') === '';
46 var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
48 re.exec = function () {
50 result.groups = { a: '7' };
53 // eslint-disable-next-line regexp/no-useless-dollar-replacements -- false positive
54 return ''.replace(re, '$<a>') !== '7';
58 fixRegExpWellKnownSymbolLogic('replace', function (_, nativeReplace, maybeCallNative) {
59 var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
62 // `String.prototype.replace` method
63 // https://tc39.es/ecma262/#sec-string.prototype.replace
64 function replace(searchValue, replaceValue) {
65 var O = requireObjectCoercible(this);
66 var replacer = searchValue == undefined ? undefined : getMethod(searchValue, REPLACE);
68 ? call(replacer, searchValue, O, replaceValue)
69 : call(nativeReplace, toString(O), searchValue, replaceValue);
71 // `RegExp.prototype[@@replace]` method
72 // https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
73 function (string, replaceValue) {
74 var rx = anObject(this);
75 var S = toString(string);
78 typeof replaceValue == 'string' &&
79 stringIndexOf(replaceValue, UNSAFE_SUBSTITUTE) === -1 &&
80 stringIndexOf(replaceValue, '$<') === -1
82 var res = maybeCallNative(nativeReplace, rx, S, replaceValue);
83 if (res.done) return res.value;
86 var functionalReplace = isCallable(replaceValue);
87 if (!functionalReplace) replaceValue = toString(replaceValue);
89 var global = rx.global;
91 var fullUnicode = rx.unicode;
96 var result = regExpExec(rx, S);
97 if (result === null) break;
99 push(results, result);
102 var matchStr = toString(result[0]);
103 if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
106 var accumulatedResult = '';
107 var nextSourcePosition = 0;
108 for (var i = 0; i < results.length; i++) {
111 var matched = toString(result[0]);
112 var position = max(min(toIntegerOrInfinity(result.index), S.length), 0);
114 // NOTE: This is equivalent to
115 // captures = result.slice(1).map(maybeToString)
116 // but for some reason `nativeSlice.call(result, 1, result.length)` (called in
117 // the slice polyfill when slicing native arrays) "doesn't work" in safari 9 and
118 // causes a crash (https://pastebin.com/N21QzeQA) when trying to debug it.
119 for (var j = 1; j < result.length; j++) push(captures, maybeToString(result[j]));
120 var namedCaptures = result.groups;
121 if (functionalReplace) {
122 var replacerArgs = concat([matched], captures, position, S);
123 if (namedCaptures !== undefined) push(replacerArgs, namedCaptures);
124 var replacement = toString(apply(replaceValue, undefined, replacerArgs));
126 replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue);
128 if (position >= nextSourcePosition) {
129 accumulatedResult += stringSlice(S, nextSourcePosition, position) + replacement;
130 nextSourcePosition = position + matched.length;
133 return accumulatedResult + stringSlice(S, nextSourcePosition);
136 }, !REPLACE_SUPPORTS_NAMED_GROUPS || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE);