.search()

mehboobali98's avatar
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:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy