| | |
| | | 'use strict'; |
| | | var apply = require('../internals/function-apply'); |
| | | var call = require('../internals/function-call'); |
| | | var uncurryThis = require('../internals/function-uncurry-this'); |
| | | var fixRegExpWellKnownSymbolLogic = require('../internals/fix-regexp-well-known-symbol-logic'); |
| | | var fails = require('../internals/fails'); |
| | | var anObject = require('../internals/an-object'); |
| | |
| | | var REPLACE = wellKnownSymbol('replace'); |
| | | var max = Math.max; |
| | | var min = Math.min; |
| | | var concat = uncurryThis([].concat); |
| | | var push = uncurryThis([].push); |
| | | var stringIndexOf = uncurryThis(''.indexOf); |
| | | var stringSlice = uncurryThis(''.slice); |
| | | |
| | | var maybeToString = function (it) { |
| | | return it === undefined ? it : String(it); |
| | |
| | | var O = requireObjectCoercible(this); |
| | | var replacer = searchValue == undefined ? undefined : getMethod(searchValue, REPLACE); |
| | | return replacer |
| | | ? replacer.call(searchValue, O, replaceValue) |
| | | : nativeReplace.call(toString(O), searchValue, replaceValue); |
| | | ? call(replacer, searchValue, O, replaceValue) |
| | | : call(nativeReplace, toString(O), searchValue, replaceValue); |
| | | }, |
| | | // `RegExp.prototype[@@replace]` method |
| | | // https://tc39.es/ecma262/#sec-regexp.prototype-@@replace |
| | |
| | | var S = toString(string); |
| | | |
| | | if ( |
| | | typeof replaceValue === 'string' && |
| | | replaceValue.indexOf(UNSAFE_SUBSTITUTE) === -1 && |
| | | replaceValue.indexOf('$<') === -1 |
| | | typeof replaceValue == 'string' && |
| | | stringIndexOf(replaceValue, UNSAFE_SUBSTITUTE) === -1 && |
| | | stringIndexOf(replaceValue, '$<') === -1 |
| | | ) { |
| | | var res = maybeCallNative(nativeReplace, rx, S, replaceValue); |
| | | if (res.done) return res.value; |
| | |
| | | var result = regExpExec(rx, S); |
| | | if (result === null) break; |
| | | |
| | | results.push(result); |
| | | push(results, result); |
| | | if (!global) break; |
| | | |
| | | var matchStr = toString(result[0]); |
| | |
| | | // but for some reason `nativeSlice.call(result, 1, result.length)` (called in |
| | | // the slice polyfill when slicing native arrays) "doesn't work" in safari 9 and |
| | | // causes a crash (https://pastebin.com/N21QzeQA) when trying to debug it. |
| | | for (var j = 1; j < result.length; j++) captures.push(maybeToString(result[j])); |
| | | for (var j = 1; j < result.length; j++) push(captures, maybeToString(result[j])); |
| | | var namedCaptures = result.groups; |
| | | if (functionalReplace) { |
| | | var replacerArgs = [matched].concat(captures, position, S); |
| | | if (namedCaptures !== undefined) replacerArgs.push(namedCaptures); |
| | | var replacement = toString(replaceValue.apply(undefined, replacerArgs)); |
| | | var replacerArgs = concat([matched], captures, position, S); |
| | | if (namedCaptures !== undefined) push(replacerArgs, namedCaptures); |
| | | var replacement = toString(apply(replaceValue, undefined, replacerArgs)); |
| | | } else { |
| | | replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue); |
| | | } |
| | | if (position >= nextSourcePosition) { |
| | | accumulatedResult += S.slice(nextSourcePosition, position) + replacement; |
| | | accumulatedResult += stringSlice(S, nextSourcePosition, position) + replacement; |
| | | nextSourcePosition = position + matched.length; |
| | | } |
| | | } |
| | | return accumulatedResult + S.slice(nextSourcePosition); |
| | | return accumulatedResult + stringSlice(S, nextSourcePosition); |
| | | } |
| | | ]; |
| | | }, !REPLACE_SUPPORTS_NAMED_GROUPS || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE); |