Javascript strings are used to represent and manipulate a sequence of characters.
You can check if string contains a substring in javascript using the string.includes(substring)
method.
Basic Example
string.includes(substring);
In this tutorial, you will learn the different methods to check If a string contains a substring in JavaScript.
Using Includes() Method
Includes() method checks if the Substring is available in the String or not.
It returns
True
– If the substring is availableFalse
– If the substring is not available
includes()
method is case sensitive. So, sub
and Sub
are different.
Use this method when you want to check if the substring exists or not and return a boolean flag as a result.
Syntax
The following syntax shows how to use the includes()
method.
string.includes(substring);
Code
let str = 'substring';
let substr = 'sub';
alert(str.includes(substr));
Output
true
Code
let str = 'substring';
let substr = 'Sub';
alert(str.includes(substr));
Output
false
Check if string contains substring in case insensitive Way
You can check if a String Contains a Substring in a case insensitive way by converting both the string and the substring to either upper
or lower
case.
Code
The following code demonstrates converts both the string to lowercase and checks if a string contains a substring.
str.toLowerCase().includes('substr'.toLowerCase());
Using Includes with Position
includes()
method also accepts start_value
parameter to mention from where the search should begin.
It’s an optional parameter.
Syntax
string.includes(substring, start_index);
Code
let str = 'Search for sub in the string';
let substr = 'sub';
alert(str.includes(substr,15));
Output
The code returns the output False
as the substring 'sub'
is not found in the given string after the position 15
.
false
Using includes() with if….else
You can use the includes
method with the if...else
statement if you want to perform different operations based on the existence of the substring.
Code
The following code shows how to use includes()
method with if...else
statement.
//the following two statements receives input from the user
let str = prompt('Enter a string');
let substr = prompt('Enter the substring that you want to check');
//the following statements performs the comparison
if(str.includes(substr)){
console.log(`The given string contains the substring ${substr}`);
}
else{
console.log(`The given string does not contain the substring ${substr}`);
}
Output
Enter a string: substring
Enter the substring that you want to check: sub
The given string contains the substring sub
Using indexOf() method
If you want to know the position of first occurrence of the substring in a given string, you can use the indexOf() method.
Returns
Index_position
– If the string is present-1
– If the Substring is not present
Like includes()
method, indexOf()
method is also case sensitive.
Use this method when you want to check if a substring exists in a string and return the index position as a result.
Syntax
string.indexOf(substring);
Code
let str = 'jsowl';
let substr = 'owl';
alert(str.indexOf(substr));
Output
2
Code
The following code demonstrates the return value when the substring is not available.
let str = 'stackvidhya';
alert(str.indexOf('sub'));
Output
-1
Using indexof With Position
In the indexof()
method, you can specify the position from where the search should begin using the offset_parameter
.
It is an optional parameter.
Syntax
string.indexOf(substring,offset_parameter);
Code
let str = 'stackvidhya';
alert(str.indexOf('vid',2));
Output
5
Using Search() Method
search() method checks the presence of a particular substring in the given string.
It returns the first position of the match even if more matches are found.
Index_position
– If the string is present-1
– If the Substring is not present
Use this method when you want to use a regular expression to check if a substring match is available in the string and return an index position.
Syntax
string.search('substring');
Code
let str = "search method is used here to check the presence of substring";
alert(str.search('sub') !== -1);
Output
true
Code
let str = "search method is used here to check the presence of substring";
alert(str.search('Sub') !== -1);
Output
false
Using Match() Method
match() is another method that checks the presence of a substring in the given string.
- It accepts a regular expression as a parameter to perform the match.
- Returns an array of all matches found in the string.
Use this method when you want to use a regular expression to check if a substring match is available in the string and return an array of all matches.
Syntax
string.match(substring);
Code
let str = 'the match method takes regular expression as the parameter';
alert(str.match(/match/)!==null);
alert(str.match(/The/i)!==null); //i is a case insensitive modifier to make the search case insensitive
Output
true
true
Using Regex Test() Method
test()
method is used to check for the substring in the given string.
- It creates a regular expression for the substring
- Compares the regex against the given string.
It returns
True
in case if the match is foundfalse
if match is not found.
Use the test()
method when you want to check if a substring exists in a string using a regular expression and return True
or False
.
Syntax
regex.test(string);
Code
let str = 'The string contains regex';
let substr = /Regex/i; ////i is a case insensitive modifier to make the search case
substr.test(str);
Output
true
JSfiddle
This tutorial is available in this JSFiddle.