replace.js 978 B

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