How to Search a String in JavaScript Using Search() Method?

Do you want to know how to search all occurrences in a string or character in JavaScript using the search() method? Read on!

Are you ready to explore the power of the JavaScript search() method?

In this article, we will show you how to use search() to find matches in a string and understand the difference between search() and indexOf().

Aside from that, you will discover the other JavaScript methods for string searching, packed with examples and explanations.

What is search() method?

The search() method is a built-in method in JavaScript that allows you to search for a match between a regular expression and a string.

It returns the index of the first match found in the string, or -1 if no match is found. This method is case-sensitive, meaning that it will treat uppercase and lowercase characters as distinct when searching for a match.

If the search value is a string, it is automatically converted to a regular expression before the search is performed.

Syntax

 string.search(searchValue)

Parameter

searchValue (Required)

The searchValue parameter of the search() method is the value that you want to search for within the string. This value can be either a regular expression or a string.

If you provide a string as the searchValue, it will be automatically converted into a regular expression before the search is performed.

Return value

The search() method returns the index of the first match found in the string, or -1 if no match is found.

How to search all occurrences in a string in JavaScript?

There are several ways to search for all occurrences in a string in JavaScript.

One way is to use the search() method as what we have mentioned earlier, which searches a string for a specified value and returns the position of the match.

Here’s an example of how to use the search() method:

let sampletext = "Hi Welcome to Itsourcecode";
let result = sampletext.search("Itsourcecode"); ✅
console.log(result)

Output:

14

Here’s an example of how to use the search() method for case insensitive:

let sampletext = "Hi Welcome to Itsourcecode";
let result = sampletext.search(/itsourcecode/i); ✅
console.log(result);

The “i” flag at the end of the regular expression makes the search case-insensitive.

Output:

14

What is the difference between search() and indexOf()?

The search() and indexOf() methods are both used to search for a substring within a string in JavaScript. Nevertheless, there are few differences between the two methods:

The search() method takes a regular expression as an argument, while the indexOf() method takes a string as an argument.

It simply means that you can use more complex search patterns with the search() method, while the indexOf() method is limited to searching for exact matches of the specified string.

The indexOf() method can take a second argument, which specifies the position in the string where the search should begin.

This allows you to start searching from a specific point in the string. However, search() method doesn’t have this option.

What are the different search string methods in JavaScript?

Here are some examples that demonstrate the use of different methods for searching for a character, string or a substring within another string in JavaScript:

Method 1: match() method

let samplestr = "Itsourcecodes provide free sourcecodes";
let result = samplestr.match(/code/g); ✅
console.log(result); 

In our first example, we use the match() method to search for all occurrences of the regular expression /code/ within the string.

The method returns an array containing all matches.

Output:

[ 'code', 'code' ]

Method 2: includes() method

let samplestr = "Welcome to Itsourcecode!";
let result = samplestr.includes("Itsourcecode"); ✅
console.log(result);

In our second example, we use the includes() method to check if the string “Welcome to Itsourcecode!” contains the substring “Itsourcecode.”

The method returns a boolean value indicating whether or not the substring was found.

Output:

true

Method 3: startsWith() method

let samplestr = "Hi, Welcome to Itsourcecode!";
let result = samplestr.startsWith("Hi"); ✅
console.log(result); 

In our third example, we use the startsWith() method to check if the string “Hi, Welcome to Itsourcecode!” starts with the substring “Hello.”

The method returns a boolean value indicating whether or not the string starts with the specified substring.

Output:

true

Method 4: endsWith() method

let samplestr = "Hi, Welcome to Itsourcecode!";
let result = samplestr.endsWith("Itsourcecode!"); ✅
console.log(result); 

In our 4rth example, we use the endsWith() method to check if the string “Hi, Welcome to Itsourcecode!” ends with the substring “Itsourcecode!

The method returns a boolean value indicating whether or not the string ends with the specified substring.

true

Method 5: indexOf() method

let samplestr = "Hi, Welcome to Itsourcecode!";
let result = samplestr.indexOf("Itsourcecode!"); ✅
console.log(result); 

In our 5th example, we use the indexOf() method to search for the first occurrence of the substring “Itsourcecode” within the string “Hi, Welcome to Itsourcecode!

The method returns the index of the first occurrence of the substring.

15

Method 6: lastIndexOf() method

let samplestr = "Hi, Welcome to Itsourcecode!";
let result = samplestr.lastIndexOf("t");
console.log(result); 

In our 6th example, we use the lastIndexOf() method to search for the last occurrence of the character “t” within the string “Hi, Welcome to Itsourcecode!

The method returns the index of the last occurrence of the character.

Conclusion

The JavaScript search() method is a useful tool for searching strings or characters, allowing you to find matches using regular expressions or simple strings.

We have explained the syntax, parameters, and return value of the search() method, and provided examples of how to use it to search for a match between a regular expression and a string.

Unlike indexOf(), which searches for exact matches, search() provides greater flexibility in specifying search patterns.

Moreover, we also explored other string searching methods such as match(), includes(), startsWith(), endsWith(), indexOf(), and lastIndexOf(), and provides examples of how to use each of these methods.

We are hoping that this article provides you with enough information. That will help you understand the search string in JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment