JavaScript (ES2026) reference · String Methods
JavaScript String search() Method
Return the index of the first regular-expression match, or -1 if there is none.
JavaScript (ES2026) Since ES3
Syntax
string.search(regexp)
Parameters
| Parameter | Description |
|---|---|
regexp |
A regular expression. |
Return value
An index, or -1.
Examples
console.log("Invoice #4821".search(/\d/), "no digits".search(/\d/));
Output
9 -1
console.log("Hello World".search(/world/i));
Output
6
Version history
| Version | Change |
|---|---|
| JavaScript ES3 | Added |
Related
string.indexOf()· Return the index of the first occurrence of some text, or -1 if it isn't found.string.match()· Match the string against a regular expression and return the matches (all of them with the g flag) or null.string.includes()· Return true if the string contains the given text anywhere. Case-sensitive.- JavaScript Strings · tutorial
- JavaScript String Methods · tutorial