gpt4 book ai didi

javascript - 比较 Javascript 中的字符串

转载 作者:行者123 更新时间:2023-12-03 04:32:03 25 4
gpt4 key购买 nike

我需要比较标题(由很多单词组成)并附上一份脏话 list 。就一个词而言,indexOf 工作得很好。但当脏话很多的时候就不行了。

谁能帮忙解决这个问题吗?

var title = "This is my title";
var badwordlist = title.indexOf('fword and other bad words list');
//var badwordlist = title.indexOf('fword');

if ( badwordlist >= 0){
//do something
}

最佳答案

我觉得到目前为止发布的两个答案有些过分

var title = "fword This is a f**king title.",
words = title.split(" "),
badwords = ["fword", "f**king"];

// one of these should do it

var isGood = words.filter(function(a) {
return badwords.indexOf(a) == -1;
});

var isBad = words.filter(function(a) {
return badwords.indexOf(a) != -1;
});

console.log(isBad, isGood);

// if isBad.length>0 then there were swear words in the title

关于javascript - 比较 Javascript 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43451018/

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