The string.match()
is a built-in function in JavaScript; it is used to search a string, for a match, against a regular expression. If it returns an Array object the match is found, if no match is found a Null value is returned.
The syntax for string.match()
is shown below:
It has two things
an expression that needs to be searched or replaced
a modifier that modifies the search
modifiers | meaning |
---|---|
g | It searches the expression at all instances. |
i | It performs case-insensitive matching. |
var data = 'Welcome to the Educative! Everyone likes Educative';// regular expression with 'g' modifiervar regExp = /Educative/g;// string.match() function.var found = data.match(regExp);// printing an array object.console.log(found);
var data = 'Welcome to the Educative! Everyone likes Educative';// regular expression with 'i' modifiervar regExp = /educative/i;// string.match() function.var found = data.match(regExp);// printing an array object.console.log(found);
var data = 'Welcome to the Educative! Everyone likes Educative';// regular expression with 'g' 'i' both modifiervar regExp = /educative/gi;// string.match() function.var found = data.match(regExp);// printing an array object.console.log(found);