JavaScript search()Method
Examples
Search for "Example":
var str="Visit Example!";
var n=str.search("Example");
nOutput:
Try it Yourself »
Definition and Usage
The search() method is used to search for a specified substring in a string, or to search for a substring that matches a regular expression.
If no matching substring is found, -1 is returned.
See more regular expression tutorialsRegExp Tutorial and our RegExp Object Reference.
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the search() method
Syntax
string.search(searchvalue)
Parameter Values
| Parameter | Description |
|---|---|
| searchvalue | Required. The string or regular expression to search for. |
Return Value
| Type | Description |
|---|---|
| Number | The starting position of the String object that matches the specified string or regular expression. |
Technical Details
| JavaScript Version: | 1.2 |
|---|
More Examples
Examples
Perform a case-sensitive search:
var str="Mr. Blue has a blue house";
document.write(str.search("blue"));
document.write(str.search("blue"));
Output of the above example:
Try it Yourself »
Examples
Perform a case-insensitive search:
var str="Mr. Blue has a blue house";
document.write(str.search(/blue/i));
document.write(str.search(/blue/i));
Output of the above example:
Try it Yourself »
Other Extensions
JavaScript String Object