Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The search() method in JavaScript is used to find the position of a substring or a regular expression match within a string. It returns the index of the first match or -1 if no match is found. Unlike indexOf(), search() supports regular expressions, making it more flexible for pattern matching.
In this article, you will learn the syntax, usage, and examples of the search() method, along with best practices for effective string searching in JavaScript.
Syntax
The syntax of the search() method is:
string.search(searchValue)
Parameters
The search() method accepts one parameter:
searchValue(Required): A string or a regular expression pattern that specifies what to search for within the given string.
Return Value
The method returns:
An integer: The index (position) of the first occurrence of the search term in the string.
-1: If the search term is not found in the string.
JavaScript String search() Method Examples
Example 1: Searching Characters with Regular Expressions in JavaScript
You can use regular expressions with the search() method to find patterns within strings.
let text = "Coding is fun and coding is powerful!";
let position = text.search(/coding/i);
console.log(position);
You can also try this code with Online Javascript Compiler
The method searches for the word "powerful" in the given string.
The word "powerful" starts at index 13, so the method returns 13.
Differences between String search() & String indexOf()
Feature
search()
indexOf()
Accepts
Regular expressions & string patterns
Only string values & does not work with regular expressions
Returns
Index of the first match found in the string, if no match is found, it returns -1
Index of the first occurrence of the specified string, if not found, returns -1
Search Scope
Always searches through the entire string & cannot start from a specific position
Can start searching from a specific position using a second parameter
Performance
Slower compared to indexOf() because it needs to process regular expressions
Faster since it only looks for exact string matches
Differences between String search() & String match()
Feature
search()
match()
Returns
Index position of the first match in the string
An array containing all matches found in the string
Multiple Occurrences
Cannot find multiple occurrences of a pattern, stops at the first match
Can find all occurrences of a pattern when used with the global flag g
Matched Text
Does not store or return the actual matched text
Returns the matched text & can include capturing groups
Use Case
Useful when we only need to know if & where a pattern exists in the string
Better when we need to collect & use the matched text in our code
Supported Browsers
The search() method is supported in all major browsers, including:
Browser
Support
Chrome
Yes
Firefox
Yes
Edge
Yes
Safari
Yes
Opera
Yes
Internet Explorer
Yes
Frequently Asked Questions
What happens if the search() method doesn’t find the search term?
If the method doesn’t find the search term, it returns -1.
Can I use case-insensitive search without regular expressions?
No, the search() method is case-sensitive unless you use a regular expression with the i flag.
How is search() different from indexOf()?
The search() method supports regular expressions, while indexOf() only works with strings.
Conclusion
In this article, we discussed the search() method in JavaScript, which is used to find the position of a substring or a regular expression match within a string. It returns the index of the first match or -1 if no match is found. Understanding the search() method helps in handling string searches efficiently in JavaScript applications.