Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The search() method of String values executes a search for a match between a regular expression and this string, returning the index of the first match in the string.
A regular expression object, or any object that has a Symbol.search method.
If regexp is not a RegExp object and does not have a Symbol.search method, it is implicitly converted to a RegExp by using new RegExp(regexp).
The index of the first match between the regular expression and the given string, or -1 if no match was found.
The implementation of String.prototype.search() doesn't do much other than calling the Symbol.search method of the argument with the string as the first parameter. The actual implementation comes from RegExp.prototype[Symbol.search]().
The g flag of regexp has no effect on the search() result, and the search always happens as if the regex's lastIndex is 0. For more information on the behavior of search(), see RegExp.prototype[Symbol.search]().
When you want to know whether a pattern is found, and also know its index within a string, use search().
The following example searches a string with two different regex objects to show a successful search (positive value) vs. an unsuccessful search (-1).
| ECMAScript® 2027 Language Specification # sec-string.prototype.search |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 10, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.