How do you check if a string contains a certain word in javascript?
In PHP, we can use the code above to check if a string contain specific words, but how can I do the same function in JavaScript/jQuery? Show
j08691 200k31 gold badges252 silver badges267 bronze badges asked Mar 22, 2011 at 8:18
Charles YeungCharles Yeung 37.8k29 gold badges87 silver badges130 bronze badges 4
you can use indexOf for this
Edit: This is an old answer that keeps getting up votes every once in a while so I thought I should clarify that in the above code, the
Update in ECMAScript2016:
answered Mar 22, 2011 at 8:22
3 indexOf/includes should not be used for finding whole words:A word (in western culture) is a drawing of a symbols close to each other, with some space between each word. A word is considered as such if it's a complete piece of characters draw together and not a partial part of it:
Check if a single word (whole word) is in the stringFind a real whole word, not just if the letters of that word are somewhere in the string.
Check if all words are in the string
answered Jan 12, 2014 at 23:41
vsyncvsync 108k54 gold badges285 silver badges371 bronze badges 6 If you are looking for exact words and don't want it to match things like "nightmare" (which is probably what you need), you can use a regex:
If you just want to find the characters "are", then use If you want to match arbitrary words, you have to programatically construct a RegExp (regular expression) object itself based on the word string and use answered Mar 22, 2011 at 8:27
Stephen ChungStephen Chung 14.4k1 gold badge33 silver badges48 bronze badges You're looking for the indexOf function:
answered Mar 22, 2011 at 8:22
evbaileyevbailey 3713 silver badges11 bronze badges You might wanna use include method in JS.
PS: includes is case sensitive. answered Apr 16, 2018 at 7:26
An easy way to do it to use Regex match() method :- For Example
Check the fiddle NOTE: For adding case sensitiveness update the regex with Thanks
Vineeth Sai 3,2827 gold badges22 silver badges32 bronze badges answered Sep 20, 2018 at 6:33
In javascript the includes() method can be used to determines whether a string contains particular word (or characters at specified position). Its case sensitive.
answered Oct 20, 2019 at 8:09
ZeniZeni 8471 gold badge11 silver badges22 bronze badges 1 This will
return
will
return answered Jun 19, 2015 at 17:07
JahidJahid 20.4k8 gold badges89 silver badges104 bronze badges
answered Aug 29, 2018 at 6:09
Using a conditional ternary operator
answered Jan 21 at 20:28
WiiLFWiiLF 1842 silver badges11 bronze badges If you'd like to find a single word in a string without regular expressions, you can do as follows:
Advantages over regex:
answered Jun 8 at 9:42
ArikArik 4,8421 gold badge25 silver badges25 bronze badges How do I search for a specific word in JavaScript?JavaScript Code:
function search_word(text, word){ var x = 0, y=0; for (i=0;i< text. length;i++) { if(text[i] == word[0]) { for(j=i;j< i+word. length;j++) { if(text[j]==word[j-i]) { y++; } if (y==word. length){ x++; } } y=0; } } return "'"+word+"' was found "+x+" times.
How do I find a specific word in a string?To find a word in the string, we are using indexOf() and contains() methods of String class. The indexOf() method is used to find an index of the specified substring in the present string. It returns a positive integer as an index if substring found else returns -1.
How do you check if a string contains a specific character in JavaScript?Use the String. includes() method to check if a string contains a character, e.g. if (str. includes(char)) {} . The include() method will return true if the string contains the provided character, otherwise false is returned.
How do you check if something is in a string JavaScript?Use the typeof operator to check if a variable is a string, e.g. if (typeof variable === 'string') . If the typeof operator returns "string" , then the variable is a string.
|