One of the most common tasks in any programming language is find out whether a string contains a given substring. JavaScript provides multiple ways to check if a string contains a substring.

In this tutorial, we will look into six different ways to check if a substring is a part of a string or not. Let's start with the most common one: indexOf() method.

1. String indexOf() Method

The most common (and perhaps the fastest) way to check if a string contains a substring is to use the indexOf() method. This method returns the index of the first occurrence of the substring. If the string does not contain the given substring, it returns -1.

The indexOf() method is case-sensitive and accepts two parameters. The first parameter is the substring to search for, and the second optional parameter is the index to start the search from (default index is 0).

var str = "The MEAN stack is MongoDB, Express.js, AngularJS, and Node.js";
str.indexOf('MongoDB') !== -1 // true
str.indexOf('Java') !== -1 //false
str.indexOf('Node', 5) !== -1 //true

To learn more about the indexOf() method, check out JavaScript String indexOf() Method guide.

2. String includes() Method

The includes() method was introduced in ES6 and works in all modern browsers except Internet Explorer. Unlike the good old indexOf() method, which returns the starting index of the substring, the includes() method returns true if the string contains the specified substring. Otherwise, it returns false.

Similar to indexOf() method, includes() is also case-sensitive and accepts an optional second parameter, an integer which indicates the position where to start searching for.

var str = "The MEAN stack is MongoDB, Express.js, AngularJS, and Node.js";
str.includes('MongoDB') //true
str.includes('Java') //false
str.includes('Node', 5) //true

To learn more about the includes() method, check out JavaScript String includes() Method guide.

3. String search() Method

The search() method searches the position of the substring in a string, and returns the position of the match. The search value can be a string or a regular expression. It returns -1 if no match is found.

var str = "The MEAN stack is MongoDB, Express.js, AngularJS, and Node.js";
str.search('MongoDB')  !== -1 //true
str.search('Java') !== -1 //false
str.search(/node/i) !== -1 //true where i is the case insensitive modifier 

4. String match() Method

Another way to check if a string contains a substring is to use the match() method. It accepts a regular expression as parameter and searches the string for a match. If it finds the matches, it returns an object, and null if no match is found.

var str = "The MEAN stack is MongoDB, Express.js, AngularJS, and Node.js";
str.match(/MongoDB/)
//["MongoDB", index: 18, input: "The MEAN stack is MongoDB, Express.js, AngularJS, and Node.js", groups: undefined]
str.match(/Java/) //null
str.match(/MongoDB/g)  !== null //true
str.match(/Java/g) !== null //false
str.match(/node/i) !== null //true where i is the case insensitive modifier 

If the regular expression does not include the g modifier, the match() method will return only the first match in the string.

5. RegExp.test() Method

The regular expression test() method checks if a match exists in a string. This method returns true if it finds a match, otherwise it returns false.

We first need to create a regular expression for the substring and then test it against the target string.

var str = "The MEAN stack is MongoDB, Express.js, AngularJS, and Node.js";
var exp = /MongoDB/g;
exp.test(str) //true
/Java/g.test(str) //false
/node/i.test(str) //true where i is the case insensitive modifier 

For more details on how to create and use regular expressions, check out Introduction to JavaScript Regular Expressions.

6. Lodash Library

Lodash is a third-party library which provides _.includes() method to check the presence of a substring in a string. This method returns true if a match is found, otherwise false.

var str = "The MEAN stack is MongoDB, Express.js, AngularJS, and Node.js";
_.includes(str,Β 'MongoDB') //true
_.includes(str,Β 'Java') //false
_.includes(str,Β 'Node') //true 

Summary

As we have seen, there are many ways to check if a string contains a substring in JavaScript. So what is the best way?

  • includes() is the easiest way to check but is limited to modern browsers. Although, you can add Polyfill support to use it in IE and other older browsers, but it means loading of an external JavaScript file.
  • Regular expressions are slow and will add unnecessary performance overhead just to perform a simple task. So they are not advisable.
  • Lodash is another good option but again requires to load an external JavaScript file.
  • If you care about speed, simply use indexOf() method. It works faster and everywhere.

If you have any question or feedback, please feel free to send me a tweet anytime.

Happy Coding 😍

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. β˜• Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.