search()

ES3+

Executes a search for a match between a regular expression and this string.

Syntax

string.search(regexp)

Parameters

regexp RegExp

A regular expression object

Return Value

number

The index of the first match, or -1 if not found

Examples

JavaScript
const str = 'Hello World';
console.log(str.search(/World/));
console.log(str.search(/xyz/));
Output:
// 6 -1

Related Methods