0

I need to check the words exist in string using javascript OR jquery.

For ex String: I need to Visit W3Schools
if I check "need w3school"==> True
if I check "need go w3school"==> False
if I check "w3schools visit"==> True
if I check "need go "==> False

It can be multiple word like 1,2 or more than 2. In short it should return "TRUE" if all the words exist in string otherwise "FALSE". Doesn't matter the sequence.

var dat=[];
if(str.indexOf($('#search').val()) != -1){
    dat[] = {name:str};
}
5
  • 1
    trying again eh.... Commented Jun 13, 2018 at 17:01
  • 2
    dat.push( {name:str} ) maybe? Commented Jun 13, 2018 at 17:03
  • @epascarello with code Commented Jun 13, 2018 at 17:03
  • stackoverflow.com/questions/13911053/… Commented Jun 13, 2018 at 17:08
  • 1
    Currently duplicate of Contain word from whole string in javascript Commented Jun 13, 2018 at 17:12

1 Answer 1

2

Try following

let str = "I need to Visit W3Schools";
str = str.toLowerCase();

function check(s) {
  return s.toLowerCase().split(" ").every(x => str.includes(x));
}
console.log(check("need w3school"));
console.log(check("need go w3school"));
console.log(check("w3schools visit"));
console.log(check("need go "));

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

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.