HTML and CSS Reference
In-Depth Information
FORMAT
array = String.match(regular_expression);
EXAMPLE
matchList = "Too much, too soon". match(/too/ig) ;
EXAMPLE 17.5
<html>
<head>
<title>The match() Method</title>
</head>
<body>
<big><font face="arial, helvetica">
<script type = "text/javascript">
1
var matchArray = new Array();
2
var string="I love the smell of clover."
3
var regex = /love/g;
4
matchArray=string.match(regex);
5
document.write("Found "+ matchArray.length
+" matches.<br />");
</script>
</font></big>
</body>
</html>
EXPLANATION
1
A new array object is created.
2
The variable called string is assigned “I love the smell of clover.”
3
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-
turned.
4
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 .
5
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.
 
Search WWH ::




Custom Search