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