Favorites

JavaScript String.prototype.search() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
String.prototype.search()
JavaScript String Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

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

const text = 'Order #4821 shipped';
console.log(text.search(/\d/));
console.log(text.search(/[A-Z]{3}/));

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee