While programming you may come across many instances where you would like to check if a substring exists in a bigger string.
These kind of searches use the String search class of algorithms.
There are quite a few string search algorithms researched in Computer Science, like
At the moment, we’ll just see what are the methods/functions in JavaScript for searching a substring in a larger string.
Note: These methods in any programming language (like JavaScript) internally use the above listed algorithms for the actual search.
We can use a few functions to know whether a string contains a substring in JavaScript. Below are the methods/functions we will use to find the same:
String.prototype.includes
String.prototype.indexOf
String.prototype.search
String.prototype.match
RegExp.prototype.test
Let’s define a main string as main_str
and a sub string as sub_str
to use.
var sub_str = "Iron"
var main_str = "I Am Iron Man"
1 string.includes(searchString[, position])
includes
will return true if the search string is found anywhere within the given string; otherwise, false if not.
main_str.includes(sub_str) // returns: true
2 string.indexOf(searchString [, fromIndex])
indexOf
will return the index of the first occurrence of searchString, or -1 if not found. An empty string searchString will match at any index between 0
and string.length
main_str.indexOf(sub_str) // returns: 5
If we ask indexOf to start searching from a given index let’s say 6, this returns a -1, since the sub_str doesn’t occur in the main_str after the 6th alphabet, including the whitespaces.
main_str.indexOf(sub_str, 6) // returns: -1
3 string.search(regexp)
search
will return the index of the first match between the regular expression and the given string; if not found, -1.
We’ll create a JavaScript RegExp Object.
The 2 ways you can create RegExp Object in JavaScript are:
1. The RegExp constructor function:new RegExp("pattern", "flags")
2. With 2 front-slashes at the Start and the End of regexp pattern:/{regexp}/{flags}
// Below regexp says, Check for a space character, if found return the index of it in the main_str
var regexp = /\s/
main_str.search(regexp) // returns: 1
4 string.match(regexp)
The match()
method retrieves the result of matching a string against a regular expression. It returns An Array Object whose contents depend on the presence or absence of the global (g
) flag, or null
if no matches are found.
// Below regexp says, return all lowercase characters between a-z
var regexp = /[a-z]/g
main_str.match(regexp) // returns: an Array of 6 elements ["m", "r", "o", "n", "a", "n"]
5 regexp.test(str)
Unlike the other methods here in this list which are methods of the String Object, test
is a method of the RegExp Object of the JavaScript language.
It returnstrue
if there is a match between the regular expression and the specified string; otherwise, false
.
// Check if this regexp pattern exists in the given string
var regexp = /\s/
regexp.test(main_str) // returns: true
And that’s all for today friends!
Hope you had a nice time reading the article! 🙂