JavaScript String search() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

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)
CallWhat 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

ParameterDescription
regexpRequired. RegExp (or value coerced with new RegExp)

Return Value

Return ValueDescription
*The value returned by string.search().

JavaScript Examples

Example 1 — Find the First Match Index

Locate the first uppercase letter with a character class.

JavaScript
const str = "hey JudE";

console.log(str.search(/[A-Z]/)); // 4
console.log(str[4]);              // "J"
Try It Yourself

How It Works

  1. Build a pattern. /[A-Z]/ matches one uppercase letter.
  2. Scan from the start. search walks the string until the first hit.
  3. Get the index. "J" sits at index 4.
  4. Read the character. Use that index on the original string if you need the char.

Example 2 — Found vs -1 (and Flag i)

Missing matches return -1; add i to ignore case.

JavaScript
const str = "hey JudE";

console.log(str.search(/[.]/));   // -1  (no period)
console.log("ABC".search(/abc/)); // -1
console.log("ABC".search(/abc/i)); // 0
Try It Yourself

How It Works

  1. Check for a miss. No "." in the string → -1.
  2. Case matters by default. /abc/ does not match "ABC".
  3. Add the i flag. /abc/i matches at index 0.
  4. Branch on the result. Use if (index !== -1) before using the index.

Example 3 — search() vs indexOf()

Strings passed to search become regexes — dots are special.

JavaScript
console.log("hello world".search("world")); // 6
console.log("file.txt".search("."));        // 0  ("." = any char!)
console.log("file.txt".search(/[.]/));      // 4  (literal dot)
console.log("file.txt".indexOf("."));       // 4  (literal substring)
Try It Yourself

How It Works

  1. Plain words often work. search("world") finds index 6.
  2. Metacharacters bite. search(".") becomes /./ and matches the first character.
  3. Escape for literals. /[.]/ (or /\./) finds the real dot at 4.
  4. Prefer indexOf for literals. No regex surprises when you just want a fixed substring.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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).

Next: slice()

Learn how to extract a portion of a string by start and end indexes.

slice() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful