| 1 | 'use strict';
|
|---|
| 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 isObject = require('../internals/is-object');
|
|---|
| 10 | var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
|
|---|
| 11 | var toLength = require('../internals/to-length');
|
|---|
| 12 | var toString = require('../internals/to-string');
|
|---|
| 13 | var requireObjectCoercible = require('../internals/require-object-coercible');
|
|---|
| 14 | var advanceStringIndex = require('../internals/advance-string-index');
|
|---|
| 15 | var getMethod = require('../internals/get-method');
|
|---|
| 16 | var getSubstitution = require('../internals/get-substitution');
|
|---|
| 17 | var getRegExpFlags = require('../internals/regexp-get-flags');
|
|---|
| 18 | var regExpExec = require('../internals/regexp-exec-abstract');
|
|---|
| 19 | var wellKnownSymbol = require('../internals/well-known-symbol');
|
|---|
| 20 |
|
|---|
| 21 | var REPLACE = wellKnownSymbol('replace');
|
|---|
| 22 | var max = Math.max;
|
|---|
| 23 | var min = Math.min;
|
|---|
| 24 | var concat = uncurryThis([].concat);
|
|---|
| 25 | var push = uncurryThis([].push);
|
|---|
| 26 | var stringIndexOf = uncurryThis(''.indexOf);
|
|---|
| 27 | var stringSlice = uncurryThis(''.slice);
|
|---|
| 28 |
|
|---|
| 29 | var maybeToString = function (it) {
|
|---|
| 30 | return it === undefined ? it : String(it);
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | // IE <= 11 replaces $0 with the whole match, as if it was $&
|
|---|
| 34 | // https://stackoverflow.com/questions/6024666/getting-ie-to-replace-a-regex-with-the-literal-string-0
|
|---|
| 35 | var REPLACE_KEEPS_$0 = (function () {
|
|---|
| 36 | // eslint-disable-next-line regexp/prefer-escape-replacement-dollar-char -- required for testing
|
|---|
| 37 | return 'a'.replace(/./, '$0') === '$0';
|
|---|
| 38 | })();
|
|---|
| 39 |
|
|---|
| 40 | // Safari <= 13.0.3(?) substitutes nth capture where n>m with an empty string
|
|---|
| 41 | var REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE = (function () {
|
|---|
| 42 | if (/./[REPLACE]) {
|
|---|
| 43 | return /./[REPLACE]('a', '$0') === '';
|
|---|
| 44 | }
|
|---|
| 45 | return false;
|
|---|
| 46 | })();
|
|---|
| 47 |
|
|---|
| 48 | var REPLACE_SUPPORTS_NAMED_GROUPS = !fails(function () {
|
|---|
| 49 | var re = /./;
|
|---|
| 50 | re.exec = function () {
|
|---|
| 51 | var result = [];
|
|---|
| 52 | result.groups = { a: '7' };
|
|---|
| 53 | return result;
|
|---|
| 54 | };
|
|---|
| 55 | // eslint-disable-next-line regexp/no-useless-dollar-replacements -- false positive
|
|---|
| 56 | return ''.replace(re, '$<a>') !== '7';
|
|---|
| 57 | });
|
|---|
| 58 |
|
|---|
| 59 | // @@replace logic
|
|---|
| 60 | fixRegExpWellKnownSymbolLogic('replace', function (_, nativeReplace, maybeCallNative) {
|
|---|
| 61 | var UNSAFE_SUBSTITUTE = REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE ? '$' : '$0';
|
|---|
| 62 |
|
|---|
| 63 | return [
|
|---|
| 64 | // `String.prototype.replace` method
|
|---|
| 65 | // https://tc39.es/ecma262/#sec-string.prototype.replace
|
|---|
| 66 | function replace(searchValue, replaceValue) {
|
|---|
| 67 | var O = requireObjectCoercible(this);
|
|---|
| 68 | var replacer = isObject(searchValue) ? getMethod(searchValue, REPLACE) : undefined;
|
|---|
| 69 | return replacer
|
|---|
| 70 | ? call(replacer, searchValue, O, replaceValue)
|
|---|
| 71 | : call(nativeReplace, toString(O), searchValue, replaceValue);
|
|---|
| 72 | },
|
|---|
| 73 | // `RegExp.prototype[@@replace]` method
|
|---|
| 74 | // https://tc39.es/ecma262/#sec-regexp.prototype-@@replace
|
|---|
| 75 | function (string, replaceValue) {
|
|---|
| 76 | var rx = anObject(this);
|
|---|
| 77 | var S = toString(string);
|
|---|
| 78 |
|
|---|
| 79 | var functionalReplace = isCallable(replaceValue);
|
|---|
| 80 | if (!functionalReplace) replaceValue = toString(replaceValue);
|
|---|
| 81 | var flags = toString(getRegExpFlags(rx));
|
|---|
| 82 |
|
|---|
| 83 | if (
|
|---|
| 84 | typeof replaceValue == 'string' &&
|
|---|
| 85 | !~stringIndexOf(replaceValue, UNSAFE_SUBSTITUTE) &&
|
|---|
| 86 | !~stringIndexOf(replaceValue, '$<') &&
|
|---|
| 87 | !~stringIndexOf(flags, 'y')
|
|---|
| 88 | ) {
|
|---|
| 89 | var res = maybeCallNative(nativeReplace, rx, S, replaceValue);
|
|---|
| 90 | if (res.done) return res.value;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | var global = !!~stringIndexOf(flags, 'g');
|
|---|
| 94 | var fullUnicode;
|
|---|
| 95 | if (global) {
|
|---|
| 96 | fullUnicode = !!~stringIndexOf(flags, 'u') || !!~stringIndexOf(flags, 'v');
|
|---|
| 97 | rx.lastIndex = 0;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | var results = [];
|
|---|
| 101 | var result;
|
|---|
| 102 | while (true) {
|
|---|
| 103 | result = regExpExec(rx, S);
|
|---|
| 104 | if (result === null) break;
|
|---|
| 105 |
|
|---|
| 106 | push(results, result);
|
|---|
| 107 | if (!global) break;
|
|---|
| 108 |
|
|---|
| 109 | var matchStr = toString(result[0]);
|
|---|
| 110 | if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | var accumulatedResult = '';
|
|---|
| 114 | var nextSourcePosition = 0;
|
|---|
| 115 | for (var i = 0; i < results.length; i++) {
|
|---|
| 116 | result = results[i];
|
|---|
| 117 |
|
|---|
| 118 | var matched = toString(result[0]);
|
|---|
| 119 | var position = max(min(toIntegerOrInfinity(result.index), S.length), 0);
|
|---|
| 120 | var captures = [];
|
|---|
| 121 | var replacement;
|
|---|
| 122 | // NOTE: This is equivalent to
|
|---|
| 123 | // captures = result.slice(1).map(maybeToString)
|
|---|
| 124 | // but for some reason `nativeSlice.call(result, 1, result.length)` (called in
|
|---|
| 125 | // the slice polyfill when slicing native arrays) "doesn't work" in safari 9 and
|
|---|
| 126 | // causes a crash (https://pastebin.com/N21QzeQA) when trying to debug it.
|
|---|
| 127 | for (var j = 1; j < result.length; j++) push(captures, maybeToString(result[j]));
|
|---|
| 128 | var namedCaptures = result.groups;
|
|---|
| 129 | if (functionalReplace) {
|
|---|
| 130 | var replacerArgs = concat([matched], captures, position, S);
|
|---|
| 131 | if (namedCaptures !== undefined) push(replacerArgs, namedCaptures);
|
|---|
| 132 | replacement = toString(apply(replaceValue, undefined, replacerArgs));
|
|---|
| 133 | } else {
|
|---|
| 134 | replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue);
|
|---|
| 135 | }
|
|---|
| 136 | if (position >= nextSourcePosition) {
|
|---|
| 137 | accumulatedResult += stringSlice(S, nextSourcePosition, position) + replacement;
|
|---|
| 138 | nextSourcePosition = position + matched.length;
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | return accumulatedResult + stringSlice(S, nextSourcePosition);
|
|---|
| 143 | }
|
|---|
| 144 | ];
|
|---|
| 145 | }, !REPLACE_SUPPORTS_NAMED_GROUPS || !REPLACE_KEEPS_$0 || REGEXP_REPLACE_SUBSTITUTES_UNDEFINED_CAPTURE);
|
|---|