How to Check if a String Contains a Specific Word in JavaScript

When working with strings in JavaScript, it is often necessary to determine whether a specific word exists within the string. This can be useful in various scenarios, such as validating user input or searching for specific keywords in a text document. In this article, we will explore different approaches to achieve this task efficiently.

To check if a string contains a specific word in javascript, we can follow these methods:

Method 1: Using the includes() Method

The includes() method is a simple and straightforward way to check if a string contains a specific word. It returns a boolean value indicating whether the word is found within the string or not. Here’s an example:

const str = "Hello, world!";
const word = "world";

const isWordIncluded = str.includes(word);

console.log(isWordIncluded); // Output: true
Check if a String Contains a Specific Word in JavaScript Example 1

In this example, we use the includes() method to determine if the string str contains the word “world”. The boolean result is then stored in the variable isWordIncluded. The output will be true since the word “world” is present in the string.

Method 2: Using Regular Expressions

Regular expressions provide a powerful way to search for patterns within strings. In JavaScript, we can use regular expressions to check if a specific word exists in a string. Here’s an example:

const str = "The quick brown fox jumps over the lazy dog";
const word = "fox";

const pattern = new RegExp("\\b" + word + "\\b", "i");
const isWordIncluded = pattern.test(str);

console.log(isWordIncluded); // Output: true
Check if a String Contains a Specific Word in JavaScript example 2

In this example, we create a regular expression pattern using the RegExp constructor. The pattern searches for the word “fox” using the \\b boundary markers to match the whole word. The "i" flag makes the search case-insensitive. The test() method checks if the pattern matches any part of the string str, and the result is stored in the variable isWordIncluded.

Method 3: Using the indexOf() Method

The indexOf() method can also be used to check if a specific word exists within a string. It returns the index of the first occurrence of the word in the string, or -1 if the word is not found. Here’s an example:

const str = "JavaScript is an awesome programming language";
const word = "awesome";

const index = str.indexOf(word);
const isWordIncluded = index !== -1;

console.log(isWordIncluded); // Output: true
Check if a String Contains a Specific Word in JavaScript example 3

In this example, we use the indexOf() method to find the index of the word “awesome” in the string str. If the word is found, indexOf() returns its index, and we assign it to the variable index. Then, we check if index is not equal to -1, which indicates that the word exists in the string.

Conclusion

Checking if a string contains a specific word is a common task in JavaScript programming. In this article, we explored three different methods to accomplish this task: using the includes() method, regular expressions, and the indexOf() method. Each method has its advantages and can be used depending on your specific requirements.

References

Available For New Project

Abdullah Al Imran

I'm Abdullah Al Imran, a Full Stack WordPress Developer ready to take your website to the next level. Whether you're looking for WordPress Theme Development, adding new features, or fixing errors in your existing site, I've got you covered. Don't hesitate to reach out if you need assistance with WordPress, PHP, or JavaScript-related tasks. I'm available for new projects and excited to help you enhance your online presence. Let's chat and bring your website dreams to life!

Leave a Comment

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