JavaScript String search() Method
Example
Search for "W3Schools":
var str = "Visit W3Schools!";
var n = str.search("W3Schools");
The result of n will be:
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The search() method searches a string for a specified value, and returns the position of the match.
The search value can be string or a regular expression.
This method returns -1 if no match is found.
Read more about regular expressions in our RegExp Tutorial and our RegExp Object Reference.
Browser Support
Method | |||||
---|---|---|---|---|---|
search() | Yes | Yes | Yes | Yes | Yes |
Syntax
string.search(searchvalue)
Parameter Values
Parameter | Description |
---|---|
searchvalue | Required. A regular expression. A string will automatically be converted to a regular expression. |
Technical Details
Return Value: | A Number, representing the position of the first occurrence of the specified searchvalue, or -1 if no match is founds |
---|---|
JavaScript Version: | 1.2 |

More Examples
Example
Perform a case-sensitive search:
var str = "Mr. Blue has a blue house";
var n = str.search("blue");
The result of n will be:
Try it Yourself »
Example
Perform a case-insensitive search:
var str = "Mr. Blue has a blue house";
var n = str.search(/blue/i);
The result of n will be:
Try it Yourself »
