Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check whether a string contains a substring in JavaScript?
JavaScript provides us with different methods to check whether a string contains a substring or not. In this article, we use the following three methods for this purpose:
-
String search() method
-
String indexOf() method
-
String includes() method
Let's explore in detail each method.
Using String search() Method
The string search() method can be used to search a text (using regular expression) in a string. It matches the string against a regular expression and returns the index (position) of the first match. It returns -1 if no match is found. It's case-sensitive.
The string search() method is an ES1 feature and is fully supported in all browsers.
Syntax
string.search(searchValue)
searchValue is a regular expression or a string (will be converted to a regular expression).
Example
You can try to run the following code to check whether the string "Tutorialspoint: Free Video Tutorial" contains a substring "point" or not using the search() method:
var re = "point";
var str = "Tutorialspoint: Free Video Tutorials";
if (str.search(re) == -1) {
document.write("Does not contain Point");
} else {
document.write("Contains Point!");
}
Contains Point!
Using String indexOf() Method
The string indexOf() method can be used to check if a string contains a substring or not. It returns the position of the first occurrence of a value (substring) in the string, otherwise it returns -1 if the value is not found.
The string indexOf() method is case-sensitive and cannot search against a regular expression. It is an ES1 feature and is fully supported in all browsers.
Syntax
string.indexOf(searchvalue, start)
Parameters
searchValue - is a string to search for.
start - the index to start from (default is 0). It's an optional parameter.
Example
In the example program below, we use string indexOf() method to check whether the string "Tutorialspoint: Free Video Tutorial" contains a substring "point" or not:
var re = "point";
var str = "Tutorialspoint: Free Video Tutorials";
if (str.indexOf(re) !== -1) {
document.write("Contains " + re);
} else {
document.write("Does not contain " + re);
}
Contains point
Using String includes() Method
The string includes() method is introduced in ES6 and is supported in all modern browsers. It returns true if a string contains a specified substring, otherwise it returns false.
The includes() method is case sensitive. This method is not supported in Internet Explorer 11 (or earlier).
Syntax
string.includes(searchvalue, start)
Parameters
searchValue - is a string to search for.
start - the index to start from (default is 0). It's an optional parameter.
Example
In the example program below, we use the string includes() method to check whether the string "Tutorialspoint: Simply Easy Learning" contains a substring "JavaScript" or not:
var re = "JavaScript";
var str = "Tutorialspoint: Simply Easy Learning";
if (str.includes(re)) {
document.write("Contains " + re);
} else {
document.write("Does not contain " + re);
}
Does not contain JavaScript
Comparison
| Method | Return Value | Case Sensitive | Regex Support | Browser Support |
|---|---|---|---|---|
| search() | Index or -1 | Yes | Yes | ES1 (All browsers) |
| indexOf() | Index or -1 | Yes | No | ES1 (All browsers) |
| includes() | true or false | Yes | No | ES6 (Modern browsers) |
Conclusion
For simple substring checking, use includes() for cleaner boolean results. Use indexOf() for older browser compatibility, and search() when you need regex pattern matching.
