JavaScript .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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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