Computer >> Computer tutorials >  >> Programming >> Javascript

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


The jQuery search() method is used to check whether a string contains a substring in JavaScript.

You can try to run the following code to check whether the string “Tutorialspoint: Free Video Tutorial” contains a substring “point” in JavaScript:

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