gpt4 book ai didi

php - 替换单词数组

转载 作者:可可西里 更新时间:2023-10-31 22:06:40 26 4
gpt4 key购买 nike

x.replace(/old/gi. 'new');
x.replace(/whatever/gi. 'whatevernew');
x.replace(/car/gi. 'boat');

有没有一种方法可以将它们组合在一个正则表达式语句和新旧单词数组中。也欢迎使用 PHP 解决方案。

最佳答案

你可以这样做:

var x = 'The old car is just whatever';
var arr = [{ old: /old/gi, new: 'new' },
{ old: /whatever/gi, new: 'whatevernew' },
{ old: /car/gi, new: 'boat' }];

for (var ii = 0; ii < arr.length; ii++) {
x = x.replace(arr[ii].old, arr[ii].new);
}

关于php - 替换单词数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6529734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com