replace.js 754 B

123456789101112131415161718192021222324252627282930
  1. var toString = require('./toString');
  2. /**
  3. * Replaces matches for `pattern` in `string` with `replacement`.
  4. *
  5. * **Note:** This method is based on
  6. * [`String#replace`](https://mdn.io/String/replace).
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 4.0.0
  11. * @category String
  12. * @param {string} [string=''] The string to modify.
  13. * @param {RegExp|string} pattern The pattern to replace.
  14. * @param {Function|string} replacement The match replacement.
  15. * @returns {string} Returns the modified string.
  16. * @example
  17. *
  18. * _.replace('Hi Fred', 'Fred', 'Barney');
  19. * // => 'Hi Barney'
  20. */
  21. function replace() {
  22. var args = arguments,
  23. string = toString(args[0]);
  24. return args.length < 3 ? string : string.replace(args[1], args[2]);
  25. }
  26. module.exports = replace;