How to check whether a string contains a substring in JavaScript?

JavascriptWeb DevelopmentFront End Technology

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
raja
Published on 02-Jan-2018 08:26:08

Advertisements