How to find if a word is there in a sentence or not using jQuery | SoloLearn: Learn to code for FREE!

+16

How to find if a word is there in a sentence or not using jQuery

Take for example- word "Hello" Input - "Hello guys" or "I won't say Hello" Output - True Input -"Hello" Output- True I have used the search bar but can't find an appropriate answer. Using JQUERY Help plz 🙁

7/31/2020 12:28:59 PM

🔰 Justin 🔰[ Exams 🎯]

17 Answers

New Answer

+16

You can use .includes( ) But its vanilla javascript if(sentence.includes(word)){ } But for jquery, look at this, https://www.w3schools.com/jquery/sel_contains.asp

+11

sentence.indexOf(word) If it is -1 then the word is not there in the sentence I think jQuery is not for this purpose https://code.sololearn.com/WwE8pN46tFCd/?ref=app

+10

See below code bro 👇👇 https://code.sololearn.com/WWAk2gr0lbJM/?ref=app

+9

S.Gopi Reddy™🔵 Please don't spam in the questions and answers section By advertising codes Please go through this https://www.sololearn.com/discuss/1316935/?ref=app

+9

Rahul Verma Yaa🤔🤔🤔 Thanks✊

+7

Såñtösh Màràvi this is js right but he wanted jQuery 🤔🤔

+7

Try regex Make an input field id string for input. Let you access it as var target Let a var pattern = " hello" ; Console.log(pattern.test.(target)); if hello in input field return true elase false

+6

maf ,Namit Jain Såñtösh Màràvi Divya Mohan , how to check for multiple input at the same time? means multiple words in a sentence 😅😅😅

+6

🔰 Justin 🔰 🎯 [ Exams 📚📓 ] I didn't understand 🤔 Can you give an example

+5

Hey Namit Jain, I think indexOf will not work in some cases where word is itself a part of another word, like "there are some people".indexOf("the") // returns 0 // but, "the" isn't present in the sentence So, other options could be .includes() or the regex which Divya Mohan gave.

+4

If you want to match hello and world both in a string use this regex /hello|world/ Similarly you can increase words using |

+3

Do you mean like You want to match two or more word in a string

+3

jQuery is already written in JS, so why not JS dude? 😅 It's simple like, sentence.includes("Hello") // returns true or false as maf had already said.

+3

Ok bro sorry😘😘😘

+2

In regex pattern use global flag for multiple match g for global flag /hello/g I hope you want to know the same

+1

What do u wanna check? Like in a form? That if a person has typed values in or not? Yes u can if ($("#namefield").val() && $("#passwordfield").val() && $("#emailfield").val()){ }

+1

S.Gopi Reddy™🔵 even though i liked ur code, this is not the place to share your codes :) people are gonna say against you or even downvote your code, so delete it from here, ok?