var input = 'How i change bad 1 bad 2 bad 3';
var word = input.split(' ').map(function(item){
return item.includes("bad");
})
console.log(word);
我陷入了我的代码......如何将坏词变成好词
最佳答案
使用 JavaScript 字符串 replace()
方法。
如果您要替换某个值,则仅替换该值的第一个实例。
var input = 'How i change bad 1 bad 2 bad 3';
input = str.replace("bad", "good");
console.log(input);
// Output: How i change good bad 2 bad 3
要替换所有出现的指定值,请使用全局 (g) 修饰符
var input = 'How i change bad 1 bad 2 bad 3';
input = str.replace(/bad/g, "good");
console.log(input);
// Output: How i change good good 2 good 3
执行全局、不区分大小写的替换:
var input = 'How i change Bad 1 bad 2 bad 3';
input = str.replace(/bad/gi, "good");
console.log(input);
// Output: How i change good good 2 good 3
关于javascript - 如何用javascript替换我的代码中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60836185/