JavaScript search() method
Example
Find "Runoob":
var
str
=
"
Visit Runoob!
"
;
var
n
=
str
.
search
(
"
Runoob
"
)
;
n Output result:
Try it »
Definition and Usage
The search() method searches for a specified substring in a string, or a substring matching a regular expression.
If no matching substring is found, it returns -1.
See more regular expression tutorials RegExp Tutorial and our RegExp Object Reference Manual.
Browser support





All major browsers support the search() method
Syntax
string
.search(
searchvalue
)
Parameter value
| Parameters | Description |
|---|---|
| searchvalue | Required. The string or regular expression to search for. |
Return Value
| Type | Description |
|---|---|
| Number | The starting position in the String object where the specified string or regular expression matches. |
Technical Details
| JavaScript version: | 1.2 |
|---|
More examples
Example
Perform a case-sensitive search:
var str="Mr. Blue has a blue house";
document.write(str.search("blue"));
The example above produces the following output:
Try it »
Example
Perform a case-insensitive search:
var str="Mr. Blue has a blue house";
document.write(str.search(/blue/i));
The example above produces the following output:
Try it »
JavaScript String object