.search()

Published Jun 23, 2021Updated Jun 19, 2023
Contribute to Docs

Takes a regular expression argument and returns either the character position of the start of the first matching substring or -1 if there is no match.

Syntax

string.search(regularExpression);

Example

Find the starting index of the substring match for our regular expression:

const programmingLanguage = 'JavaScript';
console.log(programmingLanguage.search(/java/i));
// Output: 0

Codebyte Example

The following is runnable, and demonstrates the use of the `.search()’ method:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn JavaScript on Codecademy