Sai A Sai A
Updated date Jun 12, 2023
In this blog, we will learn different techniques to check if a string contains a substring in JavaScript. This comprehensive guide examines methods such as indexOf(), includes(), regular expressions (RegExp), search(), and match().

Introduction:

Searching for substrings within a larger string is a common task in JavaScript programming. Whether you're building a web application, manipulating text data, or validating user inputs, efficiently checking if a string contains a substring is crucial. In this blog post, we will explore multiple methods to perform substring searching in JavaScript. We will discuss their pros and cons, provide code examples, and evaluate their performance. By the end of this article, you will have a comprehensive understanding of different techniques for substring searching and be able to choose the most suitable method for your specific use case.

Method 1: String.prototype.indexOf()

The first method we will explore is using the built-in indexOf() function available on JavaScript strings. This method returns the index of the first occurrence of a substring within a string, or -1 if the substring is not found. By checking if the returned index is greater than or equal to zero, we can determine if the substring exists within the string.

const string = "Hello, world!";
const substring = "world";

if (string.indexOf(substring) >= 0) {
  console.log("Substring found!");
} else {
  console.log("Substring not found!");
}

Output:

Substring found!

Method 2: String.prototype.includes()

Another approach is to use the includes() method, introduced in ECMAScript 2015 (ES6). It returns a boolean value indicating whether a substring is found within a string or not. This method provides a more intuitive and expressive way to check for substring existence.

const string = "Hello, world!";
const substring = "world";

if (string.includes(substring)) {
  console.log("Substring found!");
} else {
  console.log("Substring not found!");
}

Output:

Substring found!

Method 3: Regular Expressions (RegExp)

JavaScript's regular expressions (RegExp) provide powerful pattern-matching capabilities, making them suitable for complex substring searching. By creating a RegExp object with the desired substring pattern, we can use methods like test() or match() to check for substring presence.

const string = "Hello, world!";
const substring = "world";
const regex = new RegExp(substring);

if (regex.test(string)) {
  console.log("Substring found!");
} else {
  console.log("Substring not found!");
}

Output:

Substring found!

Method 4: String.prototype.search()

The search() method performs a regular expression search on a string and returns the index of the first match, similar to indexOf(). We can utilize this method with a regular expression or a simple substring to determine if the substring exists.

const string = "Hello, world!";
const substring = "world";

if (string.search(substring) !== -1) {
  console.log("Substring found!");
} else {
  console.log("Substring not found!");
}

Output:

Substring found!

Method 5: String.prototype.match()

Lastly, the match() method can be used to extract the matched substring or pattern from a string. By using the length property of the resulting array, we can check if any matches were found.

const string = "Hello, world!";
const substring = "world";

if (string.match(substring)?.length) {
  console.log("Substring found!");
} else {
  console.log("Substring not found!");
}

Output:

Substring found!

Conclusion:

In this blog post, we explored various methods to check if a string contains a substring in JavaScript. We discussed five different approaches: indexOf(), includes(), regular expressions (RegExp), search(), and match(). Each method has its own advantages and usage scenarios.

Comments (0)

There are no comments. Be the first to comment!!!