String.prototype.search() returns the index of the first regular-expression match in a string, or -1 if there is no match (same API as MDN String.prototype.search()). Learn how it differs from indexOf(), test(), and match(), five examples, and try-it labs.
01
Kind
Instance method
02
Returns
Index or -1
03
Input
RegExp (or coerced)
04
g flag
No effect
05
Mutates?
No
06
Baseline
Widely available
Fundamentals
Introduction
Need the position of the first regex match — not the matched text itself? search() returns that index, or -1 when nothing matches.
It is the regex-friendly cousin of indexOf(). Use indexOf for literal substrings. Use search when you need patterns such as character classes, flags, or punctuation rules.
💡
Beginner tip
Only need yes/no? Prefer /pat/.test(str). Need the matched text? Prefer str.match(/pat/). Need the index of a regex hit? Use str.search(/pat/).
Examples follow MDN String.search() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
Find punctuation and capital letters with regex.
Example 1 — Basic search()
MDN demo: find the first character that is not a word, space, or apostrophe.
JavaScript
const paragraph = "I think Ruth's dog is cuter than your dog!";
// Anything not a word character, whitespace, or apostrophe
const regex = /[^\w\s']/g;
const index = paragraph.search(regex);
index; // 41
paragraph[index]; // "!"
The character class skips letters, digits, underscores, spaces, and apostrophes. The first remaining character is the trailing "!" at index 41. The g flag does not change this.
Example 2 — Found vs Not Found
MDN demo: a successful capital-letter search vs a missing dot.
JavaScript
const str = "hey JudE";
const re = /[A-Z]/;
const reDot = /[.]/;
str.search(re); // 4 (index of "J")
str.search(reDot); // -1 (no "." in the string)
search(".") becomes /./, which matches the first character. For literal punctuation, prefer an escaped regex or indexOf().
Example 5 — Pick the Right Tool
Same sentence, three different questions.
JavaScript
const text = "Order #A42 shipped";
text.search(/#\w+/); // 6 (where is the code?)
/#\w+/.test(text); // true (does a code exist?)
text.match(/#\w+/); // ["#A42"] (what is the code?)
text.indexOf("shipped"); // 11 (literal word position)
Choose the API that matches the answer you need: index, boolean, matched text, or literal substring position.
Applications
🚀 Common Use Cases
Find pattern position — locate the first digit, email-like token, or punctuation.
Validate then slice — use the index to cut from a match onward.
Case-insensitive locate — search(/word/i) without lowercasing the whole string.
Not for literals only — prefer indexOf / includes for plain text.
Not for match content — use match / exec when you need the text.
Not for every match — use matchAll when you need all hits.
🧠 How search() Finds the Index
1
Normalize the pattern
If needed, coerce the argument to a RegExp.
Input
2
Scan from the start
Look for the first match (g / lastIndex do not change this).
Search
3
Found?
Remember the starting index of that first match.
Decide
4
✅
Return index or -1
A number ready for slicing, checks, or further parsing.
Important
📝 Notes
search() returns an index, not the matched text.
The g flag has no effect on the result.
Strings are coerced to regexes — watch out for metacharacters like ..
Prefer indexOf() for literal substrings.
Prefer test() for simple yes/no checks.
Prefer match() / matchAll() when you need the matched content.
Compatibility
Browser & Runtime Support
String.prototype.search() is Baseline Widely available — supported across modern browsers since July 2015 (and far earlier as a core language feature).
✓ Baseline · Widely available
String.search()
Safe for production in browsers and runtimes (Node.js, Deno, Bun). Use it when you need the index of a regex match.
UniversalWidely available
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
search()Excellent
Bottom line: Use String.search() to get the index of the first regex match. Remember -1 means not found, and the g flag does not change the result.
Wrap Up
Conclusion
String.prototype.search() answers one clear question: “Where does this regex first match?” Reach for it when you need an index from a pattern, and switch to indexOf, test, or match when your question is different.
String.prototype.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 searching with /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() or exec() when you need the matched text or capture groups.
No. Strings are immutable. search() only returns a number (index or -1).
Did you know?
search() mostly delegates to RegExp.prototype[Symbol.search]. That is why custom objects can implement their own search behavior — and why the g flag is ignored for the usual index result.