JavaScript > String Methods

Search string in JavaScript

How to search a string for a specified value and returns its index position in JavaScript?


Search string

Search string is an string method {search() method} in Javascript, which is used to search a string for a specified value, and returns the position (number) if specified value is found, if specified value is not found the search() method returns '-1'.

The search() method having one required parameter, i.e 'Search value' (Entering value for searching inside a string).

In JavaScript, by using search() method we can search a string for a specified value and retrurns the position of the specified value.

Searching for "DotNetFunda" in a string.

<p>Click the below button to search.</p>
<input type="button" value="Search" onclick="Function()" />
<p id="myId"></p>

<script>
    function Function() {
        var a = "This is DotNetFunda";
        var r = a.search("DotNetFunda");
        document.getElementById("myId").innerHTML = r;
    }
</script>

In the above code snippet we have given Id as "myId" to the second <p> element, there is a string with the variable a in the script code. In that string we are searching for the position of the value "DotNetFunda", for that we used .search("DotNetFunda"). Onclick of the button "Search" in the HTML code fires the function in the script code at the same time string method search() method starts searching a string for a specified value and returns the position of the specified value.

OUTPUT

Case-sensitive search in a string.

<p>Click the below button for the case sensitive search.</p>
<input type="button" value="Search" onclick="Function()" />
<p id="myId"></p>

<script>
    function Function() {
        var a = "The website dotnefunda or DOTNETFUNDA or DotNetFunda, all are same.";
        var r = a.search("DotNetFunda");
        document.getElementById("myId").innerHTML = r;
    }
</script>

Case sensitive search

In the above code snippet, we are performing a case-sensitive search in a string by using search() method. Onclick of the button fires the function in the script code, at the same time search() method perform the case-sensitive search inside the atring and returns the position of specified values inside astring.

OUTPUT

Case-insensitive search in a string.

<p>Click the below button for the case-insensitive search.</p>
<input type="button" value="Search" onclick="Function()" />
<p id="myId"></p>

<script>
    function Function() {
        var a = "The website dotnefunda or DOTNETFUNDA or DotNetFunda, all are same.";
        var r = a.search(/DotNetFunda/i);
        document.getElementById("myId").innerHTML = r;
    }
</script>

Case-insensitive search

In the above code snippet, we are performing a case-insensitive search in a string by using search() method. Onclick of the button fires the function in the script code, at the same time search() method perform the case-sensitive search inside the atring and returns the position of specified values inside astring.

OUTPUT

 Views: 5074 | Post Order: 76



Write for us






Hosting Recommendations