Javascript examples for String:search
The search() method searches a string for a specified value or a regular expression, and returns the position of the match.
This method returns -1 if no match is found.
Parameter | Description |
---|---|
searchvalue | Required. A regular expression. A string will automatically be converted to a regular expression. |
A Number, representing the position of the first occurrence of the specified searchvalue, or -1 if no match is found
The following code shows how to search a string for "blue", and display the position of the match.
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {/*from w w w. j a v a 2 s .c o m*/ var str = "Blue red blue " var n = str.search(/blue/i); document.getElementById("demo").innerHTML = n; } </script> </body> </html>