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:
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.
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
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.
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
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
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
match()method will return only the first match in the string.
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
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
- Regular expressions are slow and will add unnecessary performance overhead just to perform a simple task. So they are not advisable.
- 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 😍
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: firstname.lastname@example.org.