0

I need to compare titles (which are made of many words) with a list of bad words. For one word, indexOf works fine. but when there are many swear words it doesn't.

Can anyone help with this?

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 
}
2

4 Answers 4

1

I feel the two answers posted so far are overdoing things

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! but what i need is not to filter out bad words. The only thing I need is to detect badwords and return true or false. how can i do that?
return isBad.length>0
you are a genius ! Thanks :)
1

You can use String.prototype.includes() to check if the string contains the bad word:

var title = "fword This is title.";
var badwords = ["fword", "f**k"];

var isbad = badwords.map(function(a) {
  return title.includes(a);
});

console.log(isbad);

2 Comments

Oh! thanks @mplungjan yes string.includes also do it.
Jai, this is the closest thing to what I want. I need a true or false. But how come isbad always returns false?
0

This is a simple solution, which involves an array, a cycle and a function which coordinates these

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]);
        }
    }
}

However, the word 'assignment' contains an ugly word, which in fact is not ugly. I mean if you read only the first three characters, you will think this is an ugly word. To cope with these exceptions make sure you will not censor these as well.

Comments

-1

See this SO question. This will do:

var arr = ['banana', 'monkey banana', 'apple', 'kiwi', 'orange'];

function checker(value) {
  var prohibited = ['banana', 'apple'];

  for (var i = 0; i < prohibited.length; i++) {
    if (value.indexOf(prohibited[i]) > -1) {
      return false;
    }
  }
  return true;
}

arr = arr.filter(checker);
console.log(arr);

Obtain arr by splitting your title on space, eg title.split(" ").

Once you do any filtering you can create a filtered title with title = arr.join(" ").

9 Comments

Thanks for your reply. The issue is that my title is not the same everytime. how can I store it in an array? var title = "This is my title"; var title = tab.title;
Did you read the whole answer? At the end it says Obtain arr by splitting your title on space, eg title.split(" ")
I don't think you understand that strings are arrays themselves and can be checked using indexOf
Why are you filtering using a function that is looping? That seems like overkill
See what I mean in my answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.