plain-replace-all.js 419 B

12345678910111213141516
  1. 'use strict';
  2. var value = require('../../object/valid-value');
  3. module.exports = function (search, replace) {
  4. var index, pos = 0, str = String(value(this)), sl, rl;
  5. search = String(search);
  6. replace = String(replace);
  7. sl = search.length;
  8. rl = replace.length;
  9. while ((index = str.indexOf(search, pos)) !== -1) {
  10. str = str.slice(0, index) + replace + str.slice(index + sl);
  11. pos = index + rl;
  12. }
  13. return str;
  14. };