HTML and CSS Reference
array = String.match(regular_expression);
matchList = "Too much, too soon". match(/too/ig) ;
<title>The match() Method</title>
<big><font face="arial, helvetica">
var matchArray = new Array();
var string="I love the smell of clover."
var regex = /love/g;
document.write("Found "+ matchArray.length
+" matches.<br />");
A new array object is created.
The variable called string is assigned “I love the smell of clover.”
The regular expression called regex is assigned the search pattern love . The g mod-
ifier performs a global search: multiple occurrences of the pattern will be re-
The match() method is applied to the string. The regular expression is passed as
an argument. Each time the pattern /love/ is found in the string it will be assigned
as a new element of the array called matchArray . If the g modifier is removed, only
the first occurrence of the match will be returned, and only one element will be
assigned to the array matchArray .
The length of the array, matchArray , tells us how many times the match() method
found the pattern /love/ . See Figure 17.6.
Figure 17.6 The pattern love was found twice in the string.