JavaScript .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.

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

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

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
    • Includes 34 Courses
    • With Professional Certification
    • Beginner Friendly.
      115 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours