• JavaScript Video Tutorials

JavaScript String search() Method



The JavaScript String search() method searches a string or a regular expression in the original string and returns the index(position) of the first match. It returns -1 if no match is found.

This method is case-sensitive, which means that it treats the strings "Hi" and "hi" as two different values. However, you can make the search() method case-insensitive by using the /i flag in the regular expression.

Note − The 'g'(global) flag of regular expression has no effect on the search() method result, and the search always happens as if the regexp lastIndex is 0.

Syntax

Following is the syntax of JavaScript String search() method −

search(regexp)

Parameters

  • regexp − A regexp(regular expression) object.

Return value

This method returns an index(position) of the first match between the regexp and the given string.

Example 1

If the match is found between the regexp and the original string, this method returns an index of the first match.

In the following program, we are using the JavaScript String search() method to match and retrieve the index(or position) of the regexp "/point/i" in the original string "Welcome to Tutorials Point".

<html>
<head>
<title>JavaScript String search() Method</title>
</head>
<body>
<script>
   const str = "Welcome to Tutorials Point";
   document.write("Original String: ", str);
   let regexp = /point/i;
   document.write("<br>regexp: ", regexp);
   document.write("<br>The sub-string '", regexp, "' found at position ",str.search(regexp));
</script>    
</body>
</html>

Output

The above program returns index(position) as 21 for string "Point".

Original String: Welcome to Tutorials Point
regexp: /point/i
The sub-string '/point/i' found at position 21

Example 2

If the specified regexp is not found in the original string, the search() method returns -1.

The following is another example of the JavaScript String search() method. In this example, we use this method to match the regexp '/Hi/' in the original string "Hello World".

<html>
<head>
<title>JavaScript String search() Method</title>
</head>
<body>
<script>
   const str = "Hello World";
   document.write("Original String: ", str);
   let regexp = /Hi/;
   document.write("<br>regexp: ", regexp);
   document.write("<br>The regexp '", regexp, "' found at position ",str.search(regexp));
</script>    
</body>
</html>

Output

After executing the above, it will return -1.

Original String: Hello World
regexp: /Hi/
The regexp '/Hi/' found at position -1

Example 3

Let's use the method result in a conditional statement to check whether the match is found between the regexp "/[^\w\P']/;" and the original string "Tutorials Point".

<html>
<head>
<title>JavaScript String search() Method</title>
</head>
<body>
<script>
   const str = "Tutorials Point";
   document.write("Original String: ", str);
   let regexp = /[^\w\P']/;
   document.write("<br>regexp: ", regexp);
   let index = str.search(regexp);
   document.write("<br>Method returns: ", index, "<br>");
   if(index !=-1){
      document.write("Match found");
   }
   else{
      document.write("Not found");
   }
</script>    
</body>
</html>

Output

Following is the output of the above program −

Original String: Tutorials Point
regexp: /[^\w\P']/
Method returns: 9
Match found
Advertisements