String.prototype.search()

Returns the index of the first match of a regular expression, or -1.

Since ES1 Spec ↗

Syntax

str.search(regexp)

Parameters

NameTypeRequiredDescription
regexp RegExp Yes A regular expression. A non-RegExp value is converted via `new RegExp()`.

Returns

number — The index of the first match, or -1 if no match is found.

Examples

console.log('hello world'.search(/world/));
Output
6
console.log('abc'.search(/\d/));
Output
-1

Notes

Ignores the global (g) flag and always returns the index of the first match. Use `match`/`matchAll` if you need the matched text or groups.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.0
safari 1.0
edge 12
node 0.10

See also