Lodash - replace 方法

语法

_.replace([string=''], pattern, replacement)

用 replacement 替换字符串中模式的匹配项。

参数

  • [string=''] (string) − 要修改的字符串。

  • pattern (RegExp|string) − 要替换的模式。

  • replacement (Function|string) − 匹配替换项。

输出

  • (string) −返回修改后的字符串。

示例

var _ = require('lodash');
var result = _.replace('Hi Joe', 'Joe', 'Julie');

console.log(result);

将上述程序保存在tester.js中。运行以下命令执行该程序。

命令

\>node tester.js

输出

Hi Julie

lodash_string.html