JavaScript

How to search for a substring using a Regular Expression in JavaScript

In JavaScript, we can perform various operations on the strings and among them searching a substring in a given string is very common. But the real concern is how to search a substring using a regular expression in JavaScript? Well! String.search() is one of the most popularly used methods to search a substring using regular expression.

This post will elaborate on how to search a substring using regular expression in JavaScript, and in this regard, this post will explain the below-listed learning objectives:

  • How to search for a Substring using Regular Expression?
  • What is the search() method in JavaScript?
  • Basic Syntax
  • How does the search() method work in JavaScript?

So, let’s start!

How to search for a Substring using Regular Expression?

In JavaScript, a built-in method named search() is used to search a specific substring within a given string using regular expressions.

What is the search() method in JavaScript?

The below-listed concepts will let you understand the fundamentals of the search() method:

  • The search() is a built-in string method that is used to search a substring in a given string.
  • The search() function is case-sensitive so it will search for the perfect math only. This means the search() method will consider “Java” and “java” two different strings.
  • If the perfect match is found in the given string then the search() method will return the index of the targeted substring.
  • If the targeted substring occurs more than one time in the given string then the search() method will return the index of only the first occurrence.
  • If the targeted substring is not found in the given string then the search() method will return -1.

Basic Syntax

The below snippet will present the basic syntax of the search method in JavaScript:

givenString.search(searchingValue);

How does the search() method work in JavaScript?

It’s time to implement the search() method practically. To do that, we will utilize the search() method in different examples.

Example 1: Successful Search
In this example program, we will pass a regular expression to the search() method to locate a numeric value:

var regex = /[0-9]/;
var givenString = "linuxhint12345";
var result = givenString.search(regex);
console.log(result);

In the given string the first numeric value occurs at the 9th index so the output will be “9” as shown in the below snippet:

The output verified that the search() method returns the appropriate index.

Example 2: Case sensitive
In this example, we will search for a substring “javascript” using the search method:

var regex = /javascript/;
var givenString = "JavaScript, Java, PHP, C++, javascript";
var result = givenString.search(regex);
console.log(result);

In the given string JavaScript occurs two times firstly at the 0th index and secondly at the 28th index:

The output shows that the search() method returns “28” instead of “0”. This shows that the search() method is case-sensitive.

Example 3: Case insensitive search
We can use the “i” in the regex to search a substring irrespective of the case sensitivity.

var regex = /javascript/i;
var givenString = "JavaScript, Java, PHP, C++, javascript";
var result = givenString.search(regex);
console.log(result);

Now the search() method will search for the substring irrespective of the uppercase or lowercase:

This time the search() method returned the 0th index.

Example 4: Unsuccessful Search
Let’s search for a substring that doesn’t exist in the given string:

var regex = /Python/;
var givenString = "JavaScript, Java, PHP, C++";
var result = givenString.search(regex);
console.log(result);

When we searched for “Python” within the given string, consequently, the search method will return the following output:

The above snippet verified that the search() method returned -1, when it doesn’t find a perfect match.

Conclusion

In JavaScript, the search() method gets a regex as an argument and returns the index of the first match found in the targeted string. If the match isn’t found in the given string then the search() method will return -1. In this write-up we considered various examples to understand how to search for a substring using a Regular Expression in JavaScript.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.