+ 17

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 🙁

31st Jul 2020, 12:28 PM
Justin
Justin - avatar
17 ответов
+ 17
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
31st Jul 2020, 12:32 PM
maf
maf - avatar
+ 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
31st Jul 2020, 1:15 PM
Namit Jain
Namit Jain - avatar
+ 10
31st Jul 2020, 12:41 PM
Sâñtôsh
Sâñtôsh - avatar
+ 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
2nd Aug 2020, 2:49 AM
Justin
Justin - avatar
+ 9
Rahul Verma Yaa🤔🤔🤔 Thanks✊
2nd Aug 2020, 4:32 AM
Namit Jain
Namit Jain - avatar
+ 7
Såñtösh Màràvi this is js right but he wanted jQuery 🤔🤔
31st Jul 2020, 12:43 PM
Great Pyramids 🏜
Great Pyramids 🏜 - avatar
+ 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
31st Jul 2020, 1:07 PM
Divya Mohan
Divya Mohan - avatar
+ 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 😅😅😅
1st Aug 2020, 2:10 AM
Justin
Justin - avatar
+ 6
🔰 Justin 🔰 🎯 [ Exams 📚📓 ] I didn't understand 🤔 Can you give an example
1st Aug 2020, 2:11 AM
Namit Jain
Namit Jain - avatar
+ 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.
2nd Aug 2020, 3:33 AM
777
777 - avatar
+ 4
If you want to match hello and world both in a string use this regex /hello|world/ Similarly you can increase words using |
1st Aug 2020, 2:24 AM
Divya Mohan
Divya Mohan - avatar
+ 4
Ok bro sorry😘😘😘
2nd Aug 2020, 2:52 AM
S.Gopi Reddy™🔵
S.Gopi Reddy™🔵 - avatar
+ 3
Do you mean like You want to match two or more word in a string
1st Aug 2020, 2:21 AM
Divya Mohan
Divya Mohan - avatar
+ 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.
1st Aug 2020, 2:10 PM
777
777 - avatar
+ 2
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()){ }
1st Aug 2020, 2:12 AM
maf
maf - avatar
+ 2
In regex pattern use global flag for multiple match g for global flag /hello/g I hope you want to know the same
1st Aug 2020, 2:17 AM
Divya Mohan
Divya Mohan - avatar
+ 2
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?
2nd Aug 2020, 2:49 AM
maf
maf - avatar