es.string.replace-all.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var requireObjectCoercible = require('../internals/require-object-coercible');
  4. var isRegExp = require('../internals/is-regexp');
  5. var toString = require('../internals/to-string');
  6. var getRegExpFlags = require('../internals/regexp-flags');
  7. var getSubstitution = require('../internals/get-substitution');
  8. var wellKnownSymbol = require('../internals/well-known-symbol');
  9. var IS_PURE = require('../internals/is-pure');
  10. var REPLACE = wellKnownSymbol('replace');
  11. var RegExpPrototype = RegExp.prototype;
  12. var max = Math.max;
  13. var stringIndexOf = function (string, searchValue, fromIndex) {
  14. if (fromIndex > string.length) return -1;
  15. if (searchValue === '') return fromIndex;
  16. return string.indexOf(searchValue, fromIndex);
  17. };
  18. // `String.prototype.replaceAll` method
  19. // https://tc39.es/ecma262/#sec-string.prototype.replaceall
  20. $({ target: 'String', proto: true }, {
  21. replaceAll: function replaceAll(searchValue, replaceValue) {
  22. var O = requireObjectCoercible(this);
  23. var IS_REG_EXP, flags, replacer, string, searchString, functionalReplace, searchLength, advanceBy, replacement;
  24. var position = 0;
  25. var endOfLastMatch = 0;
  26. var result = '';
  27. if (searchValue != null) {
  28. IS_REG_EXP = isRegExp(searchValue);
  29. if (IS_REG_EXP) {
  30. flags = toString(requireObjectCoercible('flags' in RegExpPrototype
  31. ? searchValue.flags
  32. : getRegExpFlags.call(searchValue)
  33. ));
  34. if (!~flags.indexOf('g')) throw TypeError('`.replaceAll` does not allow non-global regexes');
  35. }
  36. replacer = searchValue[REPLACE];
  37. if (replacer !== undefined) {
  38. return replacer.call(searchValue, O, replaceValue);
  39. } else if (IS_PURE && IS_REG_EXP) {
  40. return toString(O).replace(searchValue, replaceValue);
  41. }
  42. }
  43. string = toString(O);
  44. searchString = toString(searchValue);
  45. functionalReplace = typeof replaceValue === 'function';
  46. if (!functionalReplace) replaceValue = toString(replaceValue);
  47. searchLength = searchString.length;
  48. advanceBy = max(1, searchLength);
  49. position = stringIndexOf(string, searchString, 0);
  50. while (position !== -1) {
  51. if (functionalReplace) {
  52. replacement = toString(replaceValue(searchString, position, string));
  53. } else {
  54. replacement = getSubstitution(searchString, string, position, [], undefined, replaceValue);
  55. }
  56. result += string.slice(endOfLastMatch, position) + replacement;
  57. endOfLastMatch = position + searchLength;
  58. position = stringIndexOf(string, searchString, position + advanceBy);
  59. }
  60. if (endOfLastMatch < string.length) {
  61. result += string.slice(endOfLastMatch);
  62. }
  63. return result;
  64. }
  65. });