There are multiple ways to check if a string contains a substring in JavaScript. You can use either String.includes()
, String.indexOf()
, String.search()
, String.match()
, regular expressions, or 3rd-party library like Lodash.
String.includes()
Method
The String.includes()
provides the most simple and popular way to check if a string contains a substring in modern JavaScript.
It was introduced in ES6 and works in all modern browsers except Internet Explorer. The String.includes()
method returns true
if the string contains the specified substring. Otherwise, it returns false
.
Here is an example:
let str = 'MongoDB, Express, React, Node'
str.includes('MongoDB') //true
str.includes('Java') //false
str.includes('Node') //true
Note that includes()
is case-sensitive and accepts an optional second parameter, an integer, which indicates the position where to start searching for.
let str = 'MongoDB, Express, React, Node'
str.includes('express') // false (Due to case-sensitive)
str.includes('React', 5) // true
Read this article to learn more about the String.includes()
method in JavaScript.
String.indexOf()
Method
The String.indexOf()
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 (the default index is 0
).
let str = 'MongoDB, Express, React, Node'
str.indexOf('MongoDB') !== -1 // true
str.indexOf('Java') !== -1 // false
str.indexOf('Node', 5) !== -1 // true
To learn more about the String.indexOf()
method, read this guide.
String.search()
Method
The String.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.
let str = 'MongoDB, Express, React, Node'
str.search('MongoDB') !== -1 // true
str.search('Java') !== -1 // false
str.search(/node/i) !== -1 // true where i is the case insensitive modifier
String.match()
Method
The String.match()
method accepts a regular expression as a parameter and searches the string for a match. If it finds the matches, it returns an object, and null
if no match is found.
let str = 'MongoDB, Express, React, Node'
str.match(/MongoDB/)
// ['MongoDB', index: 0, input: 'MongoDB, Express, React, Node', 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
RegExp.test()
Method
The RegExp.test()
method executes a search for a match between a regular expression and a specified string. It returns true
if it finds a match. Otherwise, it returns false
.
let str = 'MongoDB, Express, React, Node'
const exp = new RegExp('MongoDB', 'g')
exp.test(str) // true
/Java/g.test(str) // false
/node/i.test(str) // true where i is the case insensitive modifier
Lodash _.includes()
Method
Lodash is a third-party library that provides _.includes()
method to check the presence of a substring in a string. This method returns true
if a match is found, otherwise, false
.
let str = 'MongoDB, Express, React, Node'
_.includes(str, 'MongoDB') // true
_.includes(str, 'Java') // false
_.includes(str, 'Node') // true
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.