es.string.replace.js 4.9 KB

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