A simplistic solution is:
function contains(content, word) {
return (content.toLowerCase().indexOf('word') >= 0);
}
console.log(contains("This is the word!")); //true
console.log(contains("This is WordPress")); //true
console.log(contains("Something")); //false
As you can see, the second example is incorrect, because WordPress semantically differs from word
. So, if it's not enough to find the word, but we want to isolate it as well, then we can do something like this:
function contains(content, word) {
let pattern = new RegExp("[^a-z]" + word + "[^a-z]");
return (pattern.test(content.toLowerCase()));
}
console.log(contains("This is the word!", "word")); //true
console.log(contains("This is WordPress", "word")); //true
console.log(contains("Something", "word")); //false
In this second example, I prepend and append [^a-z]
, which means "not a letter"