{"id":133,"date":"2019-09-16T16:45:00","date_gmt":"2019-09-16T16:45:00","guid":{"rendered":"https:\/\/tutorialio.com\/?p=133"},"modified":"2025-11-04T10:31:25","modified_gmt":"2025-11-04T10:31:25","slug":"check-if-string-contains-a-specific-word-or-substring-javascript","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/check-if-string-contains-a-specific-word-or-substring-javascript\/","title":{"rendered":"Check if String Contains a Specific Word or Substring in JavaScript"},"content":{"rendered":"<p>Let\u2019s say you have a string and you want to check if it contains a specific word, character or substring. In this article, you will learn about different ways in which you can perform the check using JavaScript. Here are a few variables that we are going to use:<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">var theString = \"I have been looking for Sam.\";\nvar theWord  = \"look\";\nvar theCharacter = \"I\";\nvar theSubstring = \"for Sam\";<\/code><\/pre>\n<h2 id=\"using-stringindexof-to-check-if-string-contains-substring\">Using String.indexOf() to Check if String Contains Substring<\/h2>\n<p>The fastest way to check if a string contains a particular word or substring is with the help of <a href=\"..\/string\/index-of-method.php\">String.indexOf()<\/a> method. This method returns the index of the first occurrence of the specified substring inside the calling String object. If the substring or word is not found, it returns -1. <\/p>\n<p>This means that you can compare the value returned by the <code>indexOf()<\/code> method to see if it is equal to -1. If the value is not -1, the calling string contains the substring we are looking for.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">var theString = \"I have been looking for Sam.\";\nvar theWord  = \"looking\";\nvar theCharacter = \"I\";\nvar theSubstring = \"for Sam\";\n\n\n\/\/ Output \u2014 The word \"looking\" exists in given string.\nif (theString.indexOf(theWord) !== -1) {\n  console.log('The word \"' + theWord + '\" exists in given string.');\n}\n\n\/\/ Output \u2014 The character \"I\" exists in given string.\nif (theString.indexOf(theCharacter) !== -1) {\n  console.log('The character \"' + theCharacter + '\" exists in given string.');\n}\n\n\/\/ Output \u2014 The substring \"for Sam\" exists in given string.\nif (theString.indexOf(theSubstring) !== -1) {\n  console.log('The substring \"' + theSubstring + '\" exists in given string.');\n}<\/code><\/pre>\n<p>You are not limited to the strict inequality operator (<code>!==<\/code>), you can also use <code>&gt; -1<\/code>. This is because if the word or substring exists in the given string, the index returned would always be greater than or equal to 0. However, remember that the greater than operator (<code>&gt;<\/code>) is slower than the strict inequality operator (<code>!==<\/code>).<\/p>\n<p>One important point that should be kept in mind is that if you meant to search exactly for \u201clook\u201d in the above string, the method would still return an index greater than -1 because \u201clooking\u201d exists in the string. If you are looking for exact matches, you will have to be extra careful.<\/p>\n<p>This method is case-sensitive so you would have got -1 if you searched for \u201cLooking\u201d instead of \u201clooking\u201d.<\/p>\n<h2 id=\"using-stringincludes-to-check-if-string-contains-substring\">Using String.includes() to Check if String Contains Substring<\/h2>\n<p>You can also use the <a href=\"..\/string\/includes-method.php\">String.includes()<\/a> to check if a string contains another word, character or substring. This method will return TRUE if the substring can be found within the main string and FALSE otherwise.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">var theString = \"I have been looking for Sam.\";\nvar theWord  = \"looking\";\nvar theCharacter = \"I\";\nvar theSubstring = \"for Sam\";\n\n\/\/ Output \u2014 The word \"looking\" exists in given string.\nif (theString.includes(theWord)) {\n  console.log('The word \"' + theWord + '\" exists in given string.');\n}\n\n\/\/ Output \u2014 The character \"I\" exists in given string.\nif (theString.includes(theCharacter)) {\n  console.log('The character \"' + theCharacter + '\" exists in given string.');\n}\n\n\/\/ Output \u2014 The substring \"for Sam\" exists in given string.\nif (theString.includes(theSubstring)) {\n  console.log('The substring \"' + theSubstring + '\" exists in given string.');\n}<\/code><\/pre>\n<p>Just like <code>String.indexOf()<\/code>, this method is also case-sensitive. One major issue with this method is that the browser support for <code>String.includes()<\/code> is not as good as <code>String.indexOf()<\/code>. If you don\u2019t care about the browser support for Internet Explorer, you can use <code>String.includes()<\/code> without second thought.<\/p>\n<h2 id=\"using-stringsearch-to-check-if-string-contains-substring\">Using String.search() to Check if String Contains Substring<\/h2>\n<p><a href=\"..\/string\/search-method.php\">String.search()<\/a> is another method that you can use to check if a string contains another word, substring or character inside the main string. Unlike <code>String.indexOf()<\/code> and <code>String.include()<\/code> that we have covered so far, <code>String.search()<\/code> accepts a regular expression as its parameter. This means that you can also use it to search for complex patterns instead of basic strings.<\/p>\n<p><code>String.search()<\/code> returns the index of first match between the regular expression and given string. If no match is found it returns -1. You can compare the return value of <code>String.search()<\/code> with -1 to see if the given string has the substring you are looking for. <\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">var theString = \"I have been looking for Sam.\";\nvar theWord = \"looking\";\nvar theCharacter = \"I\";\nvar theSubstring = \"for Sam\";\nvar theWordExp  = \/looking\/g;\nvar theCharacterExp = \/I\/g;\nvar theSubstringExp = \/for Sam\/g;\n\n\/\/ Output \u2014 The word \"looking\" exists in given string.\nif (theString.search(theWordExp) !== -1) {\n  console.log('The word \"' + theWord + '\" exists in given string.');\n}\n\n\/\/ Output \u2014 The character \"I\" exists in given string.\nif (theString.search(theCharacterExp) !== -1) {\n  console.log('The character \"' + theCharacter + '\" exists in given string.');\n}\n\n\/\/ Output \u2014 The substring \"for Sam\" exists in given string.\nif (theString.search(theSubstringExp) !== -1) {\n  console.log('The substring \"' + theSubstring + '\" exists in given string.');\n}<\/code><\/pre>\n<p>If you are only looking for basic strings, I would suggest that you use either <code>String.includes()<\/code> or <code>String.indexOf()<\/code> instead of <code>String.seach()<\/code>. <\/p>\n<h2 id=\"using-stringmatch-to-check-if-string-contains-substring\">Using String.match() to Check if String Contains Substring<\/h2>\n<p>The <a href=\"..\/string\/match-method.php\">String.match()<\/a> method is used to retrieve all the matches of a regular expression inside the main string. You can use it to check the existence of another word, character or substring inside the main string by passing them as regular expressions. This method will return <code>null<\/code> if no matches are found.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">var theString = \"I have been looking for Sam.\";\nvar theWord = \"looking\";\nvar theCharacter = \"I\";\nvar theSubstring = \"for Sam\";\nvar theWordExp  = \/looking\/g;\nvar theCharacterExp = \/I\/g;\nvar theSubstringExp = \/for Sam\/g;\n\n\/\/ Output \u2014 The word \"looking\" exists in given string.\nif (theString.match(theWordExp) !== null) {\n  console.log('The word \"' + theWord + '\" exists in given string.');\n}\n\n\/\/ Output \u2014 The character \"I\" exists in given string.\nif (theString.match(theCharacterExp) !== null) {\n  console.log('The character \"' + theCharacter + '\" exists in given string.');\n}\n\n\/\/ Output \u2014 The substring \"for Sam\" exists in given string.\nif (theString.match(theSubstringExp) !== null) {\n  console.log('The substring \"' + theSubstring + '\" exists in given string.');\n}<\/code><\/pre>\n<p>Actually, you can use <code>String.match()<\/code> to get a lot more information than just check the existence of a substring. If you include the <code>g<\/code> flag in the regular expression, it returns the whole array of matched substrings in case of a match. Using it to check if a string has another substring might be overkill. Also, this method is slow compared to other substring checking methods.<\/p>\n<h2 id=\"quick-summary\">Quick Summary<\/h2>\n<p>Let\u2019s recap everything that we have covered in this tutorial.<\/p>\n<ol id=\"summary-points\">\n<li>You can use four different methods to check if a string contains another word or substring. These methods are: <code>indexOf()<\/code>, <code>includes()<\/code>, <code>search()<\/code> and <code>match()<\/code>.<\/li>\n<li>The <code>indexOf()<\/code> method will return the index of the matched string while <code>includes()<\/code> just returns a Boolean TRUE or FALSE. You can use them both without worrying too much about performance. Both these methods are case-sensitive.<\/li>\n<li>If you are planning to perform case-insensitive searches, using <code>search()<\/code> with the case-insensitive flag <code>i<\/code> might be a better idea. However, you will have to escape some special characters for the search to be successful. Using the <code>search()<\/code> method also gives you more flexibility over simple string matching methods <code>indexOf()<\/code> and <code>includes()<\/code>.<\/li>\n<li>You can also use <code>match()<\/code> to see if a string has another word or substring. However, if you just want to know if a match exists, using it might be an overkill.<\/li>\n<\/ol>\n<p>Let me know if there is anything that you would like me to clarify. Also, you are more than welcome to comment if you know other techniques to check if a string contains another word, character or substring in JavaScript.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s say you have a string and you want to check if it contains a specific word, character or substring. In this article, you will learn about different ways in which you can perform the check using JavaScript. Here are a few variables that we are going to use: JavaScript var theString = &#8220;I have [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[19,17],"class_list":["post-133","post","type-post","status-publish","format-standard","hentry","category-how-to-guides","tag-javascript","tag-strings"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=133"}],"version-history":[{"count":1,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":10237,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/133\/revisions\/10237"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}