This quick tutorial is going to cover how to check if a string contains substring in JavaScript.
1. Check If A String Contains Substring With JavaScript Built-in Functions
1.1. Using The String.prototype.includes() Method
To check if a string contains substring in JavaScript, we can use the String.prototype.includes() method. The method returns true or false as appropriate, for example:
1 2 3 |
var str = 'Espresso Cappuccino Mocha Latte'; console.log(str.includes('Espresso ')); // true console.log(str.includes('Capucino')); // false |
From the function, we can write a function to check if a string contains another string in JavaScript as follows:
1 2 3 4 5 6 7 8 9 |
var strings = {}; strings.contain = function(string, substring) { return string.indexOf(substring) != -1; }; strings.containByIncludes = function(string, substring) { return string.includes(substring); }; |
1.2. Using The String.prototype.indexOf() Method
We can use the String.prototype.indexOf() method to check if a string contains another string in Javascript. The method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found, for example:
1 2 |
'Blue Whale'.indexOf('Blue'); // returns 0 'Blue Whale'.indexOf('Blute'); // returns -1 |
Let’s try to wrap the logic into a function as follows:
1 2 3 4 5 |
var strings = {}; strings.contain = function(string, substring) { return string.indexOf(substring) != -1; }; |
1.3. Using The String.prototype.search() Method
Another approach to check if a string contains substring string in JavaScript is to use the String.prototype.search() method, for example:
1 2 3 |
var str = "Independence is happiness."; var n = str.search("happiness"); console.log(n); // n = 16 |
The method executes a search for a match between a regular expression and this String object. And it returns the index of the first match between the regular expression and the given string; if not found, -1.
Let’s turn the code into a function:
1 2 3 4 5 |
var strings = {}; strings.containBySearch = function(string, substring) { return string.search(substring) != -1; }; |
1.4. Using Regular Expression
The next way to check if a string contains another string in JavaScript is to use regular expression RegExp.prototype.test(), for example:
1 2 3 4 |
var str = 'Work hard. Dream big.'; var patt = new RegExp("Dream big"); var result = patt.test(str); console.log(result); // true |
The test() method returns true if there is a match between the regular expression and the specified string; otherwise, false.
Let’s see how we can turn the above code into a function:
1 2 3 4 5 |
var strings = {}; strings.containByRegexTest = function(string, substring) { var patt = new RegExp(substring); return patt.test(string); }; |
1.5. Using The String.prototype.match() Method
Another way we can use to check if a string contains substring in Javascript is to use the String.prototype.match() method, for example:
1 2 3 4 5 |
var str ="Life is short. Live passionately."; var patt = new RegExp("Live"); var found = str.match(patt); console.log(found); // [ 'Live', index: 15, input: 'Life is short. Live passionately.' ] |
We can turn the above code into a function:
1 2 3 4 5 |
var strings = {}; strings.containByMatch = function(string, substring) { var patt = new RegExp(substring); return string.match(patt); }; |
6. Check If A String Contains Substring In JavaScript Using 3rd Party Libraries
Beside leveraging built-in methods to check if a string contains substring in Javascript, we can use other 3rd party libraries if we use them in our projects. In this sections, we will get through Lodash,Underscore.string, two libraries which support us to check whether a string contains substring in Javascript.
6.1. Using Lodash
Lodash, a modern JavaScript utility library delivering modularity, performance & extras, provides us an includes method to check if a string contains substring in Javascript. Let’s see examples below:
1 2 |
var str = "Life is a one time offer, use it well." result = _.includes(str, 'Life'); // true |
6.2. Using Underscore.string
Underscore.string, a complete string manipulation operations for Javascript, has an include method for us to check whether a string contains substring in Javascript, for example:
1 |
include("foobar", "ob"); // => true |
7. Conclusions
The tutorial has illustrated us how to determine whether a string contains substring in Javascript by different approaches includes leveraging built-in Javascript and 3rd libraries. ES6 is getting more popular nowaday, String.prototype.includes() will be a reasonable approach. However, for those who are interested in legacy compatibility, they can use String.prototype.search(), regular expression, etc.
The sample source code can be found on my Github project. And all tests can be run with jest or mocha.
Below are other related articles:
Some Open Source JavaScript Loggers
Encode Request Parameters, Query String With Special Characters