JavaScript developers heavily depend on strings to perform all kinds of operations. Every website and web application uses string manipulations. While working with strings, often web developers need to check if a string contains substring in JavaScript. There are several ways to do this in JavaScript. In this article, we will learn how to check whether string contains substring in JavaScript.
How to Check Whether String Contains Substring in JavaScript
Here are the different ways to check whether string contains substring in JavaScript.
1. Using includes() function
The includes() function checks if a string contains a substring and returns True if the string is present. Else it returns False. It is available in every string and you can call it directly as shown below. Here is its syntax.
string.includes(substring)
Here are some examples to check if one string contains another. You can use it with literal strings or string variables.
console.log('good morning'.includes('good')) // true
var a='good morning';
var b='good';
var res =a.includes(b);
console.log(res) // true
var a='good morning';
var b='food';
var res =a.includes(b);
console.log(res) // false
In the first example, we check if a literal substring ‘good’ is present in another literal string ‘good morning’, which returns true. In second example, we check if one string variable is present in another string, which also returns true. In third case, we get false because the substring ‘food’ is not present in the original string.
Includes() function will allow you to directly check if a string contains substring.
2. Using indexOf() function
indexOf() function returns the first occurrence of substring in a string. If it is not present then it returns -1. It is also available by default for all string literals and variables. Here is its syntax.
string.indexOf(substring)
Here is an example to use indexOf().
console.log('good morning'.indexOf('good')) // prints 0
var a='good morning';
var b='good';
var res =a.indexOf(b);
console.log(res) // prints 0
var a='good morning';
var b='food';
var res =a.indexOf(b);
console.log(res) // prints -1
You can always convert the result of indexOf() into a boolean result by checking the return value of indexOf() in an if conditions as shown below.
if('good morning'.indexOf('good')>-1){
console.log(true);
}else{
console.log(false);
}
var a='good morning';
var b='good';
var res =a.indexOf(b);
if(res>-1){
console.log(true)
}
else{
console.log(false)
}
In the above examples, the output is true if the substring is present in original string, else it is false.
3. Using test() function
Both the above methods work for direct checking of substring within another string. If you want to check the presence or absence of more complex patterns or string variations, then you need to use regular expressions along with test() function. Here is its syntax.
regular_expression.test(string);
Here are some examples to illustrate how this works.
console.log(/good/.test("good morning")); // prints true
var a = "good morning";
var b = /good/;
var res = b.test(a);
console.log(res); // prints true
In the above example, the pattern ‘good’ is enclosed in /…/ to define a regular expression. Also, note that the test() function is available for regular expressions and not strings. That is why you call it on the regex pattern and pass the original string as argument.
Conclusion
In this article, we have learnt 3 main ways to check if a substring is present in another string. Using includes() is the most simple and straight forward method to check if a string contains substring. If you need to get the position of a substring in another string, then you can use indexOf(). If you want to check if a complex pattern, not just string, is present in a string, then use test() function with regular expressions.
Also read:
How to Remove Property from JavaScript Object
How to Remove Item from Array in JavaScript
How to Set Checked for Checkbox in jQuery
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.