How to check whether a string contains a substring in JavaScript?

Cover Image for How to check whether a string contains a substring in JavaScript?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Check Whether a String Contains a Substring in JavaScript?

So, you're in a JavaScript coding frenzy and suddenly you find yourself facing the challenge of checking whether a string contains a specific substring. You think to yourself, "Isn't there a built-in method for that? 🤔" Well, the short answer is no. JavaScript doesn't have a String.contains() method that you can rely on. But don't worry, brave coder! We've got you covered. 😄

The Problem: Where's String.contains()?

JavaScript offers multiple ways to check if a string contains a substring. However, it can be confusing when you're used to other programming languages that provide a direct contains() method. Let's dive into some possible solutions!

Solution 1: Using String.indexOf()

One neat way to check for a substring in JavaScript is by using the indexOf() method. This method returns the position of the first occurrence of a specified value in a string. If the substring is not found, it returns -1.

Here's an example:

const str = "Hello, World!";
const substring = "World";

if (str.indexOf(substring) !== -1) {
  console.log("Substring found!");
} else {
  console.log("Substring not found!");
}

In the above code snippet, str.indexOf(substring) checks if substring is present in str. If the index returned by indexOf() is not -1, it means the substring exists within the string.

Solution 2: Using Regular Expressions

Another powerful tool at your disposal is regular expressions. They are used for pattern matching and can be handy when searching for substrings in JavaScript.

const str = "Just another example";
const substring = /example/;

if (substring.test(str)) {
  console.log("Substring found!");
} else {
  console.log("Substring not found!");
}

In this solution, we use the regular expression example to check if it matches our string using the test() method. If it does, hurray; the substring exists!

Solution 3: Using includes()

If you're working with modern JavaScript (ES6 and onwards), you can make use of the includes() method. This method returns true or false based on whether a substring is found within the main string.

const str = "Hello, World!";
const substring = "World";

if (str.includes(substring)) {
  console.log("Substring found!");
} else {
  console.log("Substring not found!");
}

The includes() method is concise and readable, providing a straightforward approach to determine if a substring exists within a string.

Conclusion

Although JavaScript doesn't have a built-in String.contains() method, you now have multiple solutions at your fingertips! The indexOf() method, regular expressions, and includes() offer different ways to accomplish this task. Choose the one that best suits your needs and embark on your coding adventure! ✨

Now that you're armed with these techniques, go forth and conquer your string-checking challenges! Share your thoughts and favorite approaches in the comments. Happy coding! 🚀


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello