Open In App

JavaScript - How To Check Whether a String Contains a Substring?

Last Updated : 02 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Here are the different methods to check whether a string contains a substring in JavaScript.

1. Using includes() Method

The includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false otherwise.

JavaScript
let s = "JavaScript is awesome!";
let res = s.includes("awesome");
console.log(res);

Output
true
  • includes() checks if the substring ("awesome") exists within the string ("JavaScript is awesome!").
  • It returns a boolean value (true or false).
  • This method is case-sensitive, so "awesome" is different from "Awesome".

2. Using indexOf() Method

The indexOf() method searches for a substring within a string and returns the index of the first occurrence. If the substring is not found, it returns -1.

JavaScript
let s = "JavaScript is awesome!";
let res = s.indexOf("awesome") !== -1; 
console.log(res);

Output
true
  • indexOf() returns the index of the first occurrence of the substring.
  • If the substring is found, the result will be an index (>= 0), otherwise -1.
  • You can compare the result to -1 to check if the substring is present.

3. Using Regular Expressions (RegExp)

You can also use regular expressions to check if a substring exists within a string. This is especially useful when you want to perform case-insensitive checks or match more complex patterns.

JavaScript
let s = "JavaScript is awesome!";
let res = /awesome/i.test(s);
console.log(res);

Output
true
  • /awesome/i.test(str) creates a regular expression to check if the word "awesome" exists in the string, ignoring case (i flag).
  • The test() method returns a boolean (true or false).
  • Regular expressions are more powerful but can be overkill for simple substring checks.

4. Using search() Method

The search() method allows you to search for a substring or regular expression pattern within a string. It returns the index of the first match or -1 if no match is found.

JavaScript
let s = "JavaScript is awesome!";
let res = s.search("awesome") !== -1;
console.log(res);

Output
true
  • search() works similarly to indexOf() but can accept regular expressions.
  • If the substring is found, it returns the index, otherwise -1.
  • Like indexOf(), you compare the result to -1 to check for the substring.

5. Using split() Method

The split() method splits a string into an array of substrings based on a separator. You can use it to check if the string can be split into an array that includes the substring.

JavaScript
let s = "JavaScript is awesome!";
let res = s.split("awesome").length > 1;
console.log(res);

Output
true
  • split("awesome") divides the string at each occurrence of "awesome".
  • If the substring exists, the resulting array will have more than one element, and the length will be greater than 1.
  • This method can be useful for checking multiple substrings at once.

Which Approach to Choose in Which Case?

MethodWhen to UseWhy Choose It
includes()When you need a simple and modern way to check for a substring.Clean, easy-to-read syntax and good for most use cases.
indexOf()When you need the index of the first occurrence of a substring.Useful for finding the position of the substring or checking existence.
RegExpWhen you need more complex patterns or case-insensitive searches.Great for pattern matching and flexible, powerful checks.
search()When working with regular expressions or need the index.Similar to indexOf() but allows for regular expression matching.
split()When you want to check the existence of multiple substrings or split at a specific pattern.Useful for checking multiple substrings or patterns in one go.

Next Article

Similar Reads