In this article, we will learn how to check whether a string contains a substring in javascript.
There are various ways to check whether a string contains a substring in JavaScript. Here are some examples to do this.
Example 1: In this example, we use the indexOf()
Method - Old School, Classic Way. This method returns the index or position of the first occurrence of a substring within the string, otherwise, it returns -1 if no match is found. Here is source code:
// Use indexOf() Method to Check if String contains a substring
var str = "tutorialsrack.com";
if (str.indexOf('rack') > -1) { // it returns `-1` if it is not present.
console.log('Substring found') // display this
} else {
console.log('Substring not found')
}
Example 2: In this example, we will use the includes()
method to check whether the string contains the substring or not. This method returns true or false instead of an index. This method was introduced in ECMAScript 6 and works in all modern browsers except Internet Explorer.
// Use string.includes() Method to Check if String contains a substring
var str = "tutorialsrack.com";
if (str.includes('rack')) { // it returns `-1` if it is not present.
console.log('Substring found') // display this
} else {
console.log('Substring not found')
}
Example 3: In this example, we will use the search()
method to check whether the string contains the substring or not. This method returns a substring position if found, otherwise -1.
// Use search() Method to Check if String contains a substring
var str = "tutorialsrack.com";
if (str.search('rack')!==-1) { // it returns `-1` if it is not present.
console.log('Substring found') // display this
} else {
console.log('Substring not found')
}
Example 4: In this example, we will use the match()
method to check whether the string contains the substring or not. This method accepts a regular expression as a parameter and searches the string for a match. If it finds the matches, it returns an object
with details of index, substring, input, and groups, and it returns null
if no match is found.
// Use match() Method to Check if String contains a substring
var str = "we will learn MongoDB, java, Express.js, AngularJS, Vue.js, and Node.js";
console.log(str.match(/MongoDB/))
//["MongoDB", index: 14, input: "we will learn MongoDB, java, Express.js, AngularJS, Vue.js, and Node.js", groups: undefined]
console.log(str.match(/Java/)) //it returns null
console.log(str.match(/MongoDB/g) !== null) //it returns true
console.log(str.match(/Java/g) !== null) //it returns false
console.log(str.match(/node/i) !== null) //it returns true where i is the case insensitive modifier
console.log(str.match(/node/) !== null) //it returns false
Note: If the regular expression does not include the "g" modifier, the match() method will return only the first match in the string.
Note: If the regular expression does not include the "i" modifier, the match() method will return only the exact match in the string. Because the method is a case sensitive
Example 5: In this example, we will use the regular expression test()
method to check whether the string contains the substring or not. This method returns true if the match is found otherwise it returns false.
// Use test() Method to Check if String contains a substring
var str = "we will learn MongoDB, java, Express.js, AngularJS, Vue.js, and Node.js";
console.log(/MongoDB/g.test(str)) //it returns true
console.log(/Java/g.test(str)) //it returns false
console.log(/node/i.test(str)) //it returns true where i is the case insensitive modifier
Or we can use third-party libraries like lodash js to check whether a string contains a substring or not in javascript. This library contains many other utility methods that can be used in our projects. Here we can use lodash js which is well documented and well tested.
Example 6: In this example, we will use _.includes()
method from the lodash library to check whether the string contains the substring or not. This method returns true if the match is found otherwise it returns false.
// Use _.includes() Method from loadlash library to Check if String contains a substring
var str = "we will learn MongoDB, java, Express.js, AngularJS, Vue.js, and Node.js";
console.log(_.includes(str,'MongoDB')) //it returns true
console.log(_.includes(str,'SQL')) //it returns false
console.log(_.includes(str,'java')) //it returns true
As we have seen, there are various ways to check whether the string contains the substring or not in javascript. So what is the best way to do this?
include()
method is the easiest way to check whether the string contains the substring or not. But it is limited to modern browsers.indexOf()
method. This method works faster and in every browser.I hope this article will help you to understand how to check whether the string contains the substring or not in javascript.
Share your valuable feedback, please post your comment at the bottom of this article. Thank you!
Comments