How to check If string contains a substring in javascript – A Definitive guide

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.

In this tutorial, you will learn the different methods to check If a string contains a substring in JavaScript.

If You’re in Hurry…

You can check the following syntax to check if a String contains a Substring in it.

string.includes(substring);

If You Want to Understand Details, Read on…

There are different methods available to check if a String contains a substring in it.

Using Includes() Method

Includes() method checks if the Substring is available in the String or not.

Returns

True – If the substring is available
False – If the substring is not available

includes() method is case sensitive. So, sub and Sub are different.

You can 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

The following code demonstrates how to check if the substring exists in the String using the includes() method.

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 how to convert both the String and the Substring to lowercase and check the existence in a case-insensitive way.

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.

You can 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

You can also 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 IndexOf() with if…else

You can use the indexOf() method with 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 indexOf() method with if...else statement.

let str = 'this is a string';

let substr = str.indexOf('str');

if(substr!==-1){

alert('Substring found');

}

else{

alert('substring not found');

}

Output

Substring found

Using Search() Method

search() method is used to check the presence of a particular substring in the given string.

It returns the first position of the match even if more matches are found.

Return Value

Index_position – If the string is present
-1 – If the Substring is not present

Syntax

string.search('substring');

You can 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.

Code

The following code demonstrates how to use the search method to check if a substring exists in the substring.

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 used to check the presence of substring in the given string.

This method accepts a regular expression as a parameter to perform the match.

Syntax

string.match(substring);

Return Value

Return an array of all matches found in the string.

You can 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.

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.

Returns

  • True in case if the match is found
  • false if match is not found.

This method creates a regular expression for the substring; then, it is compared against the given string.

Syntax

regex.test(string);

You can 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.

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.

Conclusion

In this article, you’ve learned various methods used to check If string contains a substring in javascript using includes() method or indexOf() method and using the regular expressions.

If you have any questions, please comment below.

You May Also Like

Leave a Comment