Table of contents
1.
Introduction
2.
Syntax
3.
Parameters
4.
Return Value
5.
JavaScript String search() Method Examples
5.1.
Example 1: Searching Characters with Regular Expressions in JavaScript
5.2.
Example 2: Searching Characters without Regular Expressions in JavaScript
6.
Differences between String search() & String indexOf()
7.
Differences between String search() & String match()
8.
Supported Browsers
9.
Frequently Asked Questions
9.1.
What happens if the search() method doesn’t find the search term?
9.2.
Can I use case-insensitive search without regular expressions?
9.3.
How is search() different from indexOf()?
10.
Conclusion
Last Updated: Feb 19, 2025
Easy

JavaScript String search() Method

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

JavaScript String search() Method

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
Run Code

 

Output:

0

 

Explanation:

  • The regular expression /coding/i is used.
     
  • The i flag makes the search case-insensitive.
     
  • The method finds the first occurrence of "coding" at index 0.

Example 2: Searching Characters without Regular Expressions in JavaScript

If you don’t want to use regular expressions, you can pass a simple string as the search value.

let sentence = "JavaScript is a powerful language.";
let index = sentence.search("powerful");
console.log(index);
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

13

 

Explanation:

  • 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()

Featuresearch()indexOf()
AcceptsRegular expressions & string patternsOnly string values & does not work with regular expressions
ReturnsIndex of the first match found in the string, if no match is found, it returns -1Index of the first occurrence of the specified string, if not found, returns -1
Search ScopeAlways searches through the entire string & cannot start from a specific positionCan start searching from a specific position using a second parameter
PerformanceSlower compared to indexOf() because it needs to process regular expressionsFaster since it only looks for exact string matches

Differences between String search() & String match()

Featuresearch()match()
ReturnsIndex position of the first match in the stringAn array containing all matches found in the string
Multiple OccurrencesCannot find multiple occurrences of a pattern, stops at the first matchCan find all occurrences of a pattern when used with the global flag g
Matched TextDoes not store or return the actual matched textReturns the matched text & can include capturing groups
Use CaseUseful when we only need to know if & where a pattern exists in the stringBetter when we need to collect & use the matched text in our code

Supported Browsers

The search() method is supported in all major browsers, including:

BrowserSupport
ChromeYes
FirefoxYes
EdgeYes
SafariYes
OperaYes
Internet ExplorerYes

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.

Live masterclass