Check if String Contains a Specific Word in JavaScript

By Faruque Ahamed Mollick

Today, in this tutorial I am going to show you how to check if a string contains a specific word or not. Here I am going to show you the simple and easy JavaScript code snippets that will do this task.

I am going to print a message on the web page. The message will show depending upon if the word exists in the string or not in javaScript.

So, first of all, let’s create our HTML tag where we will show our message:

<p id="myId"></p>

By taking the ID of the element we are going to print the message.

Now see the JavaScript code below that will check if the string contains a specific word or not:



var str = "This is the string where we will check if our word exists.";
var str_pos = str.indexOf("word");
if (str_pos > -1) {
	document.getElementById("myId").innerHTML = "The specific word exists";
} else {
	document.getElementById("myId").innerHTML = "The specific word doesn't exist";
}

In the above code, I have used the indexOf() method. The JavaScript indexOf() method returns the position of specified value in a string. In this tutorial, I have used this method to check if a specific value exists in the string or not.

If the word we are looking for exists in the string, then it will return a value which will be must greater than -1. The returned value will start from 0 if the value exists in the string. If the word found at the very first of the string, then the position of this value in the string will be 0.

I have just told you that if there is no word found, then it will return -1 and if found then it will return 0 or greater than 0 depending upon the position. So, here we just have to check if the return value is greater than -1 or not.

 

Also, read:

 

If the returned value by indexOf() method is greater than -1, it means the string contains the specific word. On the other hand, if the returned value is -1 then the string doesn’t contain the word.

We have used the if else statement to show the message on the web page.

Our code is case sensitive. If you want to make it case-insensitive then you just need to add few lines of extra code. Below is our JavaScript code that will check if our string contains the specific word with case-insensitive way:

var str = "This is the string where we will check if our word exists.";
var str_lower = str.toLowerCase();
var word_find = "Word"; // Word to check
var word_lower = word_find.toLowerCase();
var str_pos = str_lower.indexOf(word_lower);
if (str_pos > -1) {
	document.getElementById("myId").innerHTML = "The specific word exists";
} else {
	document.getElementById("myId").innerHTML = "The specific word doesn't exists";
}

 

If you run the above code then it will show the message “The specific word exists” as our word will exist in the string. This is because our word “will” exist in the string. But if it the word doesn’t exist in the string, then it will show the message “The specific word doesn’t exist” on the page.

So we have successfully able to check if a string contains a specific word in JavaScript.

 

Leave a Reply

Your email address will not be published. Required fields are marked *