2 var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
3 var anObject = require('../internals/an-object');
4 var toLength = require('../internals/to-length');
5 var toInteger = require('../internals/to-integer');
6 var requireObjectCoercible = require('../internals/require-object-coercible');
7 var advanceStringIndex = require('../internals/advance-string-index');
8 var getSubstitution = require('../internals/get-substitution');
9 var regExpExec = require('../internals/regexp-exec-abstract');
14 var maybeToString = function (it) {
15 return it === undefined ? it : String(it);
19 fixRegExpWellKnownSymbolLogic('replace', 2, function (REPLACE, nativeReplace, maybeCallNative, reason) {
20 var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = reason.REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE;
21 var REPLACE_KEEPS_$0 = reason.REPLACE_KEEPS_$0;
22 var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
25 // `String.prototype.replace` method
26 // https://tc39.es/ecma262/#sec-string.prototype.replace
27 function replace(searchValue, replaceValue) {
28 var O = requireObjectCoercible(this);
29 var replacer = searchValue == undefined ? undefined : searchValue[REPLACE];
30 return replacer !== undefined
31 ? replacer.call(searchValue, O, replaceValue)
32 : nativeReplace.call(String(O), searchValue, replaceValue);
34 // `RegExp.prototype[@@replace]` method
35 // https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
36 function (regexp, replaceValue) {
38 (!REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE && REPLACE_KEEPS_$0) ||
39 (typeof replaceValue === 'string' && replaceValue.indexOf(UNSAFE_SUBSTITUTE) === -1)
41 var res = maybeCallNative(nativeReplace, regexp, this, replaceValue);
42 if (res.done) return res.value;
45 var rx = anObject(regexp);
48 var functionalReplace = typeof replaceValue === 'function';
49 if (!functionalReplace) replaceValue = String(replaceValue);
51 var global = rx.global;
53 var fullUnicode = rx.unicode;
58 var result = regExpExec(rx, S);
59 if (result === null) break;
64 var matchStr = String(result[0]);
65 if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
68 var accumulatedResult = '';
69 var nextSourcePosition = 0;
70 for (var i = 0; i < results.length; i++) {
73 var matched = String(result[0]);
74 var position = max(min(toInteger(result.index), S.length), 0);
76 // NOTE: This is equivalent to
77 // captures = result.slice(1).map(maybeToString)
78 // but for some reason `nativeSlice.call(result, 1, result.length)` (called in
79 // the slice polyfill when slicing native arrays) "doesn't work" in safari 9 and
80 // causes a crash (https://pastebin.com/N21QzeQA) when trying to debug it.
81 for (var j = 1; j < result.length; j++) captures.push(maybeToString(result[j]));
82 var namedCaptures = result.groups;
83 if (functionalReplace) {
84 var replacerArgs = [matched].concat(captures, position, S);
85 if (namedCaptures !== undefined) replacerArgs.push(namedCaptures);
86 var replacement = String(replaceValue.apply(undefined, replacerArgs));
88 replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue);
90 if (position >= nextSourcePosition) {
91 accumulatedResult += S.slice(nextSourcePosition, position) + replacement;
92 nextSourcePosition = position + matched.length;
95 return accumulatedResult + S.slice(nextSourcePosition);