JavaScript search()
Method
Example
Search for "tutorialpro":
var str = "Visit tutorialpro!";
var n = str.search("tutorialpro");
n Output:
var str = "Visit tutorialpro!";
var n = str.search("tutorialpro");
document.write(n);
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 matches are found, it returns -1.
Learn more about regular expressions in our RegExp Tutorial and our RegExp Object Reference.
Browser Support
All major browsers support the search()
method.
Syntax
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 search string or regular expression. |
Technical Details
| JavaScript Version: | 1.2 | | --- | --- |
More Examples
Example
Perform a case-sensitive search:
Output of the above example:
Example
Perform a case-insensitive search:
Output of the above example: