JavaScript - How To Check Whether a String Contains a Substring?
Last Updated :
02 Dec, 2024
Here are the different methods to check whether a string contains a substring in JavaScript.
1. Using includes() Method
The includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false otherwise.
JavaScript
let s = "JavaScript is awesome!";
let res = s.includes("awesome");
console.log(res);
- includes() checks if the substring ("awesome") exists within the string ("JavaScript is awesome!").
- It returns a boolean value (true or false).
- This method is case-sensitive, so "awesome" is different from "Awesome".
2. Using indexOf() Method
The indexOf() method searches for a substring within a string and returns the index of the first occurrence. If the substring is not found, it returns -1.
JavaScript
let s = "JavaScript is awesome!";
let res = s.indexOf("awesome") !== -1;
console.log(res);
- indexOf() returns the index of the first occurrence of the substring.
- If the substring is found, the result will be an index (>= 0), otherwise -1.
- You can compare the result to -1 to check if the substring is present.
3. Using Regular Expressions (RegExp)
You can also use regular expressions to check if a substring exists within a string. This is especially useful when you want to perform case-insensitive checks or match more complex patterns.
JavaScript
let s = "JavaScript is awesome!";
let res = /awesome/i.test(s);
console.log(res);
- /awesome/i.test(str) creates a regular expression to check if the word "awesome" exists in the string, ignoring case (i flag).
- The test() method returns a boolean (true or false).
- Regular expressions are more powerful but can be overkill for simple substring checks.
4. Using search() Method
The search() method allows you to search for a substring or regular expression pattern within a string. It returns the index of the first match or -1 if no match is found.
JavaScript
let s = "JavaScript is awesome!";
let res = s.search("awesome") !== -1;
console.log(res);
- search() works similarly to indexOf() but can accept regular expressions.
- If the substring is found, it returns the index, otherwise -1.
- Like indexOf(), you compare the result to -1 to check for the substring.
5. Using split() Method
The split() method splits a string into an array of substrings based on a separator. You can use it to check if the string can be split into an array that includes the substring.
JavaScript
let s = "JavaScript is awesome!";
let res = s.split("awesome").length > 1;
console.log(res);
- split("awesome") divides the string at each occurrence of "awesome".
- If the substring exists, the resulting array will have more than one element, and the length will be greater than 1.
- This method can be useful for checking multiple substrings at once.
Which Approach to Choose in Which Case?
Method | When to Use | Why Choose It |
---|
includes() | When you need a simple and modern way to check for a substring. | Clean, easy-to-read syntax and good for most use cases. |
indexOf() | When you need the index of the first occurrence of a substring. | Useful for finding the position of the substring or checking existence. |
RegExp | When you need more complex patterns or case-insensitive searches. | Great for pattern matching and flexible, powerful checks. |
search() | When working with regular expressions or need the index. | Similar to indexOf() but allows for regular expression matching. |
split() | When you want to check the existence of multiple substrings or split at a specific pattern. | Useful for checking multiple substrings or patterns in one go. |
Similar Reads
How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri
1 min read
Check If An Array of Strings Contains a Substring in JavaScript Here are the different ways to check if an array of strings contains a substring in JavaScript1. Using includes() and filter() methodWe will create an array of strings, then use includes() and filter() methods to check whether the array elements contain a sub-string.JavaScriptconst a = ['Geeks', 'gf
4 min read
How to check a string is entirely made up of the same substring in JavaScript ? We are given a string and the task is to determine whether the string is made up of the same substrings or not. 1. Using Regular Expression with test() method Initialize a string to the variable.Use the test() method to test a pattern in a string.The test() method returns true if the match is found,
3 min read
Javascript Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output
2 min read
PHP to check substring in a string In this article, we will see how to check the substring in a string. We are given two strings & we have to check whether the second string is a substring of the first string or not using PHP built-in strpos() function. This function is case-sensitive, which means that it treats upper-case and lo
2 min read