Home >Web Front-end >Front-end Q&A >How to use search() in javascript
In JavaScript, the search() method is used to retrieve a specified substring in a string, or to retrieve a substring that matches a regular expression, and returns the position of the first occurrence of the substring. The syntax "string .search(searchvalue)"; if no matching substring is found, "-1" is returned.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
javascript search() method--search string
The search() method is used to retrieve the specified substring in the string, Or retrieve a substring that matches a regular expression.
Syntax:
string.search(searchvalue)
Parameter description:
Return value:
Example:
Perform a case-sensitive search<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p id="demo">单击显示查找的位置</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var str="Mr. Blue has a blue house" var n=str.search("blue"); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
## Perform a case-ignoring search
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <p id="demo">单击显示查找的位置</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var str="Mr. Blue has a blue house" var n=str.search(/blue/i); document.getElementById("demo").innerHTML=n; } </script> </body> </html>
[Related recommendations:
javascript learning tutorialThe above is the detailed content of How to use search() in javascript. For more information, please follow other related articles on the PHP Chinese website!