String.prototype.search(regexp) returns the index of the first regex match, or -1 if nothing matches. It does not change the string. Prefer indexOf() for plain literal text.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call string.search. Start with this line.
JavaScript
str.search(regexp)
Call
What it does
str.search(regexp)
Primary signature.
string.search(...)
Call with the arguments your case needs.
Built into JavaScript. Call search(...) on a string (or String instance) — no import required.
Library declarations
JavaScript
str.search(regexp)
Parameters
Parameter
Description
regexp
Required. RegExp (or value coerced with new RegExp)
Return Value
Return Value
Description
*
The value returned by string.search().
JavaScript Examples
Example 1 — Find the First Match Index
Locate the first uppercase letter with a character class.
Plain words often work.search("world") finds index 6.
Metacharacters bite.search(".") becomes /./ and matches the first character.
Escape for literals./[.]/ (or /\./) finds the real dot at 4.
Prefer indexOf for literals. No regex surprises when you just want a fixed substring.
Compatibility
Browser & Runtime Support
String.prototype.search() has been in JavaScript since the early language and is universal across browsers and runtimes.
✓ Baseline · Universal
String.prototype.search()
Supported in all modern browsers, Internet Explorer, and every current Node.js release. Safe everywhere.
100%Browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
String.search()Universal
Bottom line: Use confidently in any environment. Pair with indexOf() for literal searches and match() when you need the matched text.
Frequently Asked Questions
search(regexp) returns the index of the first match of a regular expression in the string, or -1 if nothing matches.
No. The global flag has no effect on search(). It always finds the first match as if lastIndex were 0.
Yes. Non-RegExp values are converted with new RegExp(regexp), so search("cat") behaves like /cat/. Special regex characters in the string still have regex meaning.
Use indexOf() for a literal substring. Use search() when you need a regular expression pattern (character classes, case flags, word boundaries, and so on).
Use test() for a true/false answer. Use search() when you need the match index. Use match() when you need the matched text or capture groups.
No. Strings are immutable. search() only returns a number (index or -1).