How to check if a String contains a Substring in JavaScript

Q. What is a Substring?

A. A substring is a part of a larger string. For example, the string “I like cats” has the substring “like” in it.

Q. How do you check if a string contains a substring in JavaScript?

A. JavaScript offers different ways to perform this operation.

  1. Check if a String contains a Substring with String.includes()
  2. Check if a String contains a Substring with String.indexOf()
  3. Check if a String contains a Substring with Regex

Check if a String contains a Substring with String.includes

The String.includes() method can be used to check if a string contains a substring. If the substring is contained in the string, the method will return true. Otherwise, false will be returned.

const string = 'hello world';
const substring = 'hello';

console.log(string.includes(substring)); 

if (string.includes(substring)) {
  // inset your logic here 
}
Check if a String contains a Substring with String.includes() method
Check if a String contains a Substring with String.includes

ℹ️ The includes() method searches for one string within another string, returning true or false as appropriate. The search is case-sensitive, meaning that uppercase and lowercase letters are treated differently.

The code is checking if the string “hello world” includes the substring “hello”. The result will be either true or false.

To do a case insensitive check if a substring is contained in a string, convert both strings to lowercase.

Example –

const string = 'HELLO world';
const substring = 'hello';

console.log(
  string.toLowerCase().includes(substring.toLowerCase())
);

if (string.toLowerCase().includes(substring.toLowerCase())) {
  // Your logic here
}
Convert both strings to lowercase before operation.
Convert both strings to lowercase before operation.

Check if a String contains a Substring with String.indexOf()

To check if a substring is in a JavaScript string:

  1. Use the indexOf method on the string, with the substring as a parameter – string.indexOf(substring).
  2. If the returned value is not equal to -1, then the string contains the substring.

Example –

const string = 'hello world';
const substring = 'hello';

const index = string.indexOf(substring);
console.log(index);

if (string.indexOf(substring) !== -1) {
    // Your logic here
}
Checking if the string contains the substring using String.indexOf() method
Checking if the string contains the substring using String.indexOf() method.

ℹ️ The String.indexOf method returns the starting index of the substring. If the substring is not contained in the string, the indexOf method returns -1.

Explanation – The code above is checking if the string contains the substring. If it does, it will return the index of the substring within the string. Otherwise, it will return -1.

  • The first line creates a variable called string that contains the value ‘hello world’.
  • The second line creates a variable called substring that contains the value ‘hello’.
  • The third line creates a variable called index that contains the value of the first occurrence of the substring ‘hello’ within the string ‘hello world’.
  • The fourth line prints the value of the index variable to the console.
  • The fifth line checks if the substring ‘hello’ exists within the string ‘hello world’. If it does, the code inside the if statement will be executed.

Check if a String contains a Substring with Regex

One way to check if a string contains a substring is to use the RegExp.test() method. This method takes a regular expression as an argument and returns a Boolean value indicating whether or not the regex matched a section of the string.

 let str = 'Hello World';
 /Hello/.test(str);
To check if a string contains a substring, use the RegExp.test() method in a case-insensitive manner.
To check if a string contains a substring, use the RegExp.test() method in a case-insensitive manner.

The i flag allows you to do case insensitive searches.

 let str = 'Hello World';
 /hello/i.test(str);
To check if a string contains a substring, use the RegExp.test() method.
To check if a string contains a substring, use the RegExp.test() method.

We can use regex to test if a string contains a zip code (5 digit postal code), without caring what the specific zip code is. This problem cannot be solved using the includes() or indexOf() methods. However, we can use regular expressions to test this easily.

let str = 'My zip code is 5201';
console.log(/d{4}/.test(str));

str = 'My address is 123 london.';
console.log(/d{5}/.test(str));
Easy regex implementation
Easy regex implementation.

ℹ️ JavaScript is not particularly fast, so using a regex method will be slower than a simpler method like includes(). Only use this method for more complex cases where a simpler solution won’t work.

Check if a String contains a Substring in JavaScript.

Conclusion

There are a few different ways to check if a string contains a substring in JavaScript. This is a common task that is performed in many programming languages, especially when dealing with user-facing code. Checking if a string contains a substring can be useful for validating input, for example.