JavaScript String search()
Example
Search for "W3Schools":
let str = "Visit W3Schools!";
str.search("W3Schools") // Returns 6
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.
The search() method returns -1 if no match is found.
Read more about regular expressions in our RegExp Tutorial and our RegExp Object Reference.
Browser Support
search() is fully supported in all browsers:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | 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 found |
|---|---|
| JavaScript Version: | ECMAScript 1 |
More Examples
Example
Perform a case-sensitive search:
let str = "Mr. Blue has a blue house";
str.search("blue") // Returns 15
Try it Yourself »
Example
Perform a case-insensitive search:
let str = "Mr. Blue has a blue house";
str.search(/blue/i) // Returns 4
Try it Yourself »

