Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较Javascript中的字符串_Javascript_String_Compare - Fatal编程技术网

比较Javascript中的字符串

比较Javascript中的字符串,javascript,string,compare,Javascript,String,Compare,我需要比较由许多单词组成的标题 有一大堆坏话。一句话,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 somethin

我需要比较由许多单词组成的标题 有一大堆坏话。一句话,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 arr=[‘香蕉’、‘猴子香蕉’、‘苹果’、‘猕猴桃’、‘橙子’]; 函数校验值{ 禁止变量=['香蕉','苹果']; 对于变量i=0;i<0.length;i++{ 如果值.indexOfprohibited[i]>-1{ 返回false; } } 返回true; } arr=arr.filterchecker; console.logarr 您可以使用String.prototype.includes检查字符串是否包含坏单词:

var title=fword这是标题。; 变量badwords=[fword,f**k]; var isbad=badwords.mapFunction{ 返回title.includesa; };
console.logisbad 我觉得目前发布的两个答案有些过头了

var title=fword这是一个国王头衔。, words=title.split, 坏话=[fword,f**king]; //其中一个应该可以 var isGood=words.filterFunction a{ 返回badwords.indexOfa==-1; }; var isBad=words.filterFunction a{ 返回badwords.indexOfa!=-1; }; console.logisBad,很好;
//如果isBad.length>0,则标题中有脏话这是一个简单的解决方案,它包括一个数组、一个循环和一个函数来协调这些内容

var badwords = ['fword', 'uglyword'];
var replacements = ['*word', 'ug***ord'];
function replaceBadwords(title) {
    for (var badwordIndex in badwords) {
        if (title.indexOf(badwords[badwordIndex]) >= 0) {
            title = title.split(badwords[badwordIndex]).join(replacements[badwordIndex]);
        }
    }
}

然而,“分配”这个词包含了一个丑陋的词,实际上并不丑陋。我的意思是,如果你只看前三个字,你会认为这是一个丑陋的词。为了应对这些例外情况,请确保您不会对这些例外情况进行审查。

在标题上循环使用脏话。indexOftitleword[i]而不是以其他方式绕过可能重复的感谢回复。问题是我的头衔每次都不一样。如何将其存储在阵列中?var title=这是我的标题;var title=tab.title;你看了全部答案了吗?最后它说通过在空格上拆分你的标题来获得arr,例如title.split我想你不明白字符串本身就是数组,可以使用indexofw进行检查。你为什么使用循环函数进行过滤?这看起来太过分了明白我的回答是什么意思了吗!谢谢@mplungjan yes string.includes也这么做。Jai,这是最接近我想要的东西。我需要一个正确或错误的答案。但是为什么isbad总是返回false?谢谢!但我需要的不是过滤掉不好的字眼。我唯一需要做的就是发现坏话并返回真或假。我该怎么做?回程不好。长度>0你真是个天才!谢谢: