
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
1. 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. It 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” in or not. We use string search() method.
<html> <head> <title>JavaScript String search() Method</title> </head> <body> <script> 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!" ); } </script> </body> </html>
Output
This will produce the following result -
Contains Point!"
2. 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. This method cannot search against a regular expression.
The string indexOf() method is an ES1 feature. It 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.
<html> <head> <title>JavaScript String search() Method</title> </head> <body> <script> 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); } </script> </body> </html>
Output
This will produce the following result -
Contains point
3. String includes() method
The string includes() method is introduced in ES6. It 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.
<html> <head> <title>JavaScript String search() Method</title> </head> <body> <script> var re = "JavaScript"; var str = "Tutorialspoint: Simply Easy Learning"; if ( str.includes(re)) { document.write("Contains "+ re ); } else { document.write("Does not contain "+ re); } </script> </body> </html>
Output
This will produce the following result -
Does not contain JavaScript
- Related Articles
- How to check whether a string contains a substring in jQuery?
- How to check whether a String contains a substring or not?
- How to check if a string contains a substring in Golang?
- Check if a String Contains a Substring in Linux
- How to check whether an input string contains a specified substring ignoring the case using JSP?
- Java Program to Check if a string contains a substring
- Golang program to check if a string contains a substring
- How to check if an input string contains a specified substring in JSP?
- PHP 8 – Using str_contains() to check if a string contains a substring
- Golang Program to check a string contains a specified substring or not
- How do we check if a String contains a substring (ignoring case) in Java?
- How to check in C# whether the string array contains a particular work in a string array?
- How do we check in Python whether a string contains only numbers?
- PHP to check substring in a string
- How to check if string or a substring of string starts with substring in Python?
