Home >>JavaScript String Methods >JavaScript String search() Method

JavaScript String search() Method

JavaScript String search() Method

JavaScript search() method is used to search any specified value in a given input string and return the position of the match. The specified search value can be a string or a regular expression.

Syntax:

string.search(searchvalue)

Parameter Values

Parameter Description
searchvalue This is a required parameter. It defines a regular expression.

Browser Support

Method Chrome Edge Firefox Safari Opera
search() Yes Yes Yes Yes Yes

Here is an example of search() method:

<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str = "Hello!! Welcome to PHPTPOINT.."; 
  var n = str.search("PHP");
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

 

Example 2:

 

<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function myFunction1() {
  var str = "Hello everyone!! Welcome to PHPTPOINT."
  var n = str.search(/php/i);
  document.getElementById("demo1").innerHTML = n;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

 


No Sidebar ads