2 var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic');
3 var anObject = require('../internals/an-object');
4 var toObject = require('../internals/to-object');
5 var toLength = require('../internals/to-length');
6 var toInteger = require('../internals/to-integer');
7 var requireObjectCoercible = require('../internals/require-object-coercible');
8 var advanceStringIndex = require('../internals/advance-string-index');
9 var regExpExec = require('../internals/regexp-exec-abstract');
13 var floor = Math.floor;
14 var SUBSTITUTION_SYMBOLS = /\$([$&'`]|\d\d?|<[^>]*>)/g;
15 var SUBSTITUTION_SYMBOLS_NO_NAMED = /\$([$&'`]|\d\d?)/g;
17 var maybeToString = function (it) {
18 return it === undefined ? it : String(it);
22 fixRegExpWellKnownSymbolLogic('replace', 2, function (REPLACE, nativeReplace, maybeCallNative, reason) {
23 var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = reason.REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE;
24 var REPLACE_KEEPS_$0 = reason.REPLACE_KEEPS_$0;
25 var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
28 // `String.prototype.replace` method
29 // https://tc39.github.io/ecma262/#sec-string.prototype.replace
30 function replace(searchValue, replaceValue) {
31 var O = requireObjectCoercible(this);
32 var replacer = searchValue == undefined ? undefined : searchValue[REPLACE];
33 return replacer !== undefined
34 ? replacer.call(searchValue, O, replaceValue)
35 : nativeReplace.call(String(O), searchValue, replaceValue);
37 // `RegExp.prototype[@@replace]` method
38 // https://tc39.github.io/ecma262/#sec-regexp.prototype-@@replace
39 function (regexp, replaceValue) {
41 (!REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE && REPLACE_KEEPS_$0) ||
42 (typeof replaceValue === 'string' && replaceValue.indexOf(UNSAFE_SUBSTITUTE) === -1)
44 var res = maybeCallNative(nativeReplace, regexp, this, replaceValue);
45 if (res.done) return res.value;
48 var rx = anObject(regexp);
51 var functionalReplace = typeof replaceValue === 'function';
52 if (!functionalReplace) replaceValue = String(replaceValue);
54 var global = rx.global;
56 var fullUnicode = rx.unicode;
61 var result = regExpExec(rx, S);
62 if (result === null) break;
67 var matchStr = String(result[0]);
68 if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
71 var accumulatedResult = '';
72 var nextSourcePosition = 0;
73 for (var i = 0; i < results.length; i++) {
76 var matched = String(result[0]);
77 var position = max(min(toInteger(result.index), S.length), 0);
79 // NOTE: This is equivalent to
80 // captures = result.slice(1).map(maybeToString)
81 // but for some reason `nativeSlice.call(result, 1, result.length)` (called in
82 // the slice polyfill when slicing native arrays) "doesn't work" in safari 9 and
83 // causes a crash (https://pastebin.com/N21QzeQA) when trying to debug it.
84 for (var j = 1; j < result.length; j++) captures.push(maybeToString(result[j]));
85 var namedCaptures = result.groups;
86 if (functionalReplace) {
87 var replacerArgs = [matched].concat(captures, position, S);
88 if (namedCaptures !== undefined) replacerArgs.push(namedCaptures);
89 var replacement = String(replaceValue.apply(undefined, replacerArgs));
91 replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue);
93 if (position >= nextSourcePosition) {
94 accumulatedResult += S.slice(nextSourcePosition, position) + replacement;
95 nextSourcePosition = position + matched.length;
98 return accumulatedResult + S.slice(nextSourcePosition);
102 // https://tc39.github.io/ecma262/#sec-getsubstitution
103 function getSubstitution(matched, str, position, captures, namedCaptures, replacement) {
104 var tailPos = position + matched.length;
105 var m = captures.length;
106 var symbols = SUBSTITUTION_SYMBOLS_NO_NAMED;
107 if (namedCaptures !== undefined) {
108 namedCaptures = toObject(namedCaptures);
109 symbols = SUBSTITUTION_SYMBOLS;
111 return nativeReplace.call(replacement, symbols, function (match, ch) {
113 switch (ch.charAt(0)) {
114 case '$': return '$';
115 case '&': return matched;
116 case '`': return str.slice(0, position);
117 case "'": return str.slice(tailPos);
119 capture = namedCaptures[ch.slice(1, -1)];
123 if (n === 0) return match;
125 var f = floor(n / 10);
126 if (f === 0) return match;
127 if (f <= m) return captures[f - 1] === undefined ? ch.charAt(1) : captures[f - 1] + ch.charAt(1);
130 capture = captures[n - 1];
132 return capture === undefined ? '' : capture;