.search()

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);

Examples

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

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

Return a -1 if no match is found:

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

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn JavaScript on Codecademy

Contributors