1

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?

1
  • each word or sentence? Strike that..... I see now.. Commented Aug 15, 2012 at 12:06

4 Answers 4

1

Like this

var words = "I like cookies".replace(/\W/g, '|');
var sentence = "I_like_chocolate_cookies";

console.log(new RegExp(words).test(sentence));

https://tinker.io/447b7/1

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

Comments

0

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!

Comments

0

like this?

var x ="i like grape";
var c ="i_don't_like";
var xaar = x.split(' ');
for(i=0;i<xaar.length;i++){
if(c.indexOf(xaar[i])>-1) console.log(xaar[i]);

}

Comments

0
var foo = "I_like_chocolate_cookies";
var bar = "I like chocolate cookies";

foo.split('_').filter(function(elements) {
  var duplicates = []
  if(bar.split().indexOf(element) != -1) {
    return true;
  }
});

Comments

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.