JavaScript String.prototype.search() Method
The search() method returns the index of the first match of a regular expression in a string, or -1.
Description
The search() method finds the position of the first match of a regular expression, like a regex version of indexOf(). It ignores the g flag. If you only need to know whether there's a match, RegExp.prototype.test() is clearer.
Because it always searches from the start of the string and ignores the g flag, search() doesn't change the regular expression's lastIndex. A string argument is converted to a regular expression, so characters such as . and ( have their regex meanings.
To get the matched text or capture groups rather than just the position, use match() or exec(). To find a plain substring, indexOf() is simpler and avoids regex escaping.
Syntax
string.search(regexp)Parameters
- regexpA regular expression (a string is converted to one).
Return Value
The index of the first match, or -1 if there isn't one.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.search() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





