The Wayback Machine - https://web.archive.org/web/20250117224721/https://www.geeksforgeeks.org/javascript-string-search-method/
Open In App

JavaScript String search() Method

Last Updated : 23 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The search() method in JavaScript is used to search for a specified substring within a string. It returns the index of the first occurrence of the substring within the string. If the substring is not found, it returns -1.

Syntax

string.search( A )

Parameters

This method accepts a single parameter A which holds the regular expression as an object.

Return Value

This method returns the index of the first match string in between the regular expression and the given string object and returns -1 if no match is found. Indexing starts from zero (0) and in the first attempt, an alphabet is matched, then it does not check further. Simply, it returns the index of that first matched alphabet.

JavaScript String search() Method Examples

Example 1: Searching Characters with Regular Expressions in JavaScript

The code utilizes the search() method with regular expressions to find the index of matching characters within the string “GeeksforGeeks”. It prints the index of the first occurrence of ‘G’, ‘e’, and ‘s’ respectively.

JavaScript
// Taking input a string.
let string = "GeeksforGeeks";

// Taking a regular expression.
let re1 = /G/;
let re2 = /e/;
let re3 = /s/;

// Printing the index of matching alphabets
console.log(string.search(re1));
console.log(string.search(re2));
console.log(string.search(re3));

Output
0
1
4

Example 2: Searching Characters without Regular Expressions in JavaScript

JavaScript
let str = "GeeksforGeeks";
let searchString = "for";
let Result = str.search(searchString);
console.log(Result);

Output
5

Supported Browsers

JavaScript String search() Method – FAQs

How does the search() method work?

The search() method takes a regular expression or a string as an argument and searches the string for a match. It returns the position of the first occurrence of the match, or -1 if no match is found.

Does the search() method modify the original string?

No, the search() method does not modify the original string. It only searches for the specified value and returns the index of the first match or -1.

How does the search() method handle case sensitivity?

The search() method is case-sensitive. For example, “Hello, world!”.search(“World”) returns -1 because the case does not match.

How does the search() method handle Unicode characters?

The search() method correctly handles Unicode characters. If you need to ensure Unicode matching, you can use the u flag in the regular expression.

What are some common use cases for the search() method?

Common use cases for the search() method include:

  • Finding the position of a substring within a string.
  • Searching for patterns in strings using regular expressions.
  • Validating input by checking for the presence of specific characters or patterns.

We have a complete list of Javascript Strings methods, to check those please go through Javascript String Complete reference article.

Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!


Next Article

Similar Reads

three90RightbarBannerImg