Imagine you’re building a search feature for a website. A user types something into the search bar, and you need to check if their input exists in your database of articles. Or think about a messaging app where you want to highlight certain words in a conversation. In both cases, you need to check if a string contains a specific substring.In this tutorial we are going to learn about it.
Check If a String Contains a Substring in JavaScript
In JavaScript, checking whether a string contains a specific substring is a common task. This operation is useful for various applications, such as searching for keywords in user input, filtering data, or validating form inputs. JavaScript provides several methods to achieve this, but the most modern and straightforward way is by using the includes() method. However, there are alternative approaches, such as indexOf(), search(), and RegExp.Test(), which offer flexibility depending on the use case.
Let’s explore these methods with example codes!.
1)Using the includes() Method
The includes() method is the most readable and modern approach for checking whether a string contains a specific substring. It returns a boolean (true or false) depending on whether the substring is found.
let mainString = "Hello, welcome to CodeSpeedy!"; let subString = "CodeSpeedy"; if (mainString.includes(subString)) { console.log("Substring found!"); } else { console.log("Substring not found."); }
Input:
- “Hello, welcome to CodeSpeedy!”-This is the main string.
- The substring “CodeSpeedy” is present in the main string.
Ouput:
Substring found!
2) Using the indexOf() Method
The indexOf() method returns the index (position) of the first occurrence of the substring in the main string. If the substring is not found, it returns -1.
let mainString = "Hello, How are you doing!"; let subString = "CodeSpeedy"; if (mainString.indexOf(subString) !== -1) { console.log("Substring found!"); } else { console.log("Substring not found."); }
Input:
- “Hello, How are you doing!”-This is the main String
- The substring “CodeSpeedy” is not present in the main string.
Output:
Substring not found.
3)Using search() with Regular Expressions
The search() method allows you to use regular expressions (RegExp) to check if a substring exists. Like indexOf(), it returns the index of the match or -1 if the substring is missing.
let mainString = "Hello, welcome to CodeSpeedy!"; let subString = /CodeSpeedy/; // Using a RegExp if (mainString.search(subString) !== -1) { console.log("Substring found!"); } else { console.log("Substring not found."); }
Input:
-
“Hello, welcome to CodeSpeedy!”, /CodeSpeedy/
- The substring CodeSpeedy is found in the string.
Output:
Substring found!
4)Using RegExp.test() (Regular Expressions)
The test() method from the RegExp object is another way to check for substrings. It returns a boolean (true or false).
let mainString = "Hello, welcome to CodeSpeedy!"; let subString = /CodeSpeedy/; if (subString.test(mainString)) { console.log("Substring found!"); } else { console.log("Substring not found."); }
Input:
- “Hello, welcome to CodeSpeedy!”, /CodeSpeedy/
- The substring “CodeSpeedy” is present in the string.
Output:
Substring found!
If you need a simple and modern substring check, use includes() (returns true/false). To find the position of a substring, use indexOf() (returns index or -1). For regular expression searches, use search() (returns index or -1) or RegExp.test() (returns true/false).
If you want to learn more , You can also explore:
🔗 MDN Web Docs: String Methods
🔗 Regular Expressions in JavaScript