In this tutorial, we will look into 6 different ways to find out whether 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
indexOf() method. This method returns the position of the first occurrence of the substring. If no substring is found, 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 position to start the search (default to 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
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,
includes() method returns true if the string contains the specified characters, and false if not.
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
search() method searches the position of the substring in a string, and returns the position of the match. The search value can be 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
match()method will return only the first match in the string, if the regular expression does not include the
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
Lodash is a third-party library which provides
_.includes() method to check whether a string contains another string or not. 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 😍