Let's sat I have the sentence "I like cookies" and the sentence "I_like_chocolate_cookies". How do I split the string "I like cookies" and check if the words exists in the second sentence?
4 Answers
Like this
var words = "I like cookies".replace(/\W/g, '|');
var sentence = "I_like_chocolate_cookies";
console.log(new RegExp(words).test(sentence));
Comments
Here's some sample code:
str1 = 'I like cookies'
str2 = 'I_like_chocolate_cookies'
// We convert the strings to lowercase to do a case-insensitive check. If we
// should be case sensitive, remove the toLowerCase().
str1Split = str1.toLowerCase().split(' ')
str2Lower = str2.toLowerCase()
for (var i = 0; i < str1Split.length; i++) {
if (str2Lower.indexOf(str1Split[i]) > -1) {
// word exists in second sentence
}
else {
// word doesn't exist
}
}
Hope this helps!