How to use Search in JavaScript

In this article I am going to explain about search() method in JavaScript.
  • 1741

JavaScript Search() Method

JavaScript Search() method use for search any value and Regular expression and after search its will return search position. If values does not match then it will return -1.

Syntax

string.search(searchvaluename)

Example

In this example we will find the position of "Friends"

<html>

<head>

    <script type="text/javascript">

        function myfun() {

            var str = "Hello Friends";

            var st = str.search("Friends");

            document.getElementById("test").innerHTML = st;

        }

    </script>

</head>

<body>

    <p id="test">

        click button for find the position</p>

    <button onclick="myfun()">

        click</button>

</body>

</html>

Output

search.jpg

Further Readings

You may also want to read these related articles: here
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.