HTML and CSS Reference
In-Depth Information
EXAMPLE 17.8 ( CONTINUED )
4
splitArray=string.split(regex); // Split the string by colons
5
for(i=0; i < splitArray.length; i++){
document.write( splitArray[i] + "<br />");
}
</script>
</body>
</html>
EXPLANATION
1
A new array object is created.
2
The variable called string is assigned a colon-delimited string of text.
3
The variable called regex is assigned the regular expression /:/ .
4
The String object's split() method splits the string using colons as the string delim-
iter (marks the separation between words), and creates an array called splitArray .
5
Each of the array elements is displayed in the page. See Figure 17.9.
Figure 17.9 The string is split on colons.
EXAMPLE 17.9
<html>
<head>
<title>The split() Method</title>
</head>
<script type = "text/javascript">
1
var splitArray = new Array();
2
var myString="apples
pears,peaches:plums,oranges";
3
var regex = /[\t:,]/; // Delimeter is a tab, colon, or comma
4
splitArray=myString.split(regex);
for(i=0; i < splitArray.length; i++){
5
document.write( splitArray[i] + "<br />");
}
</script>
</body>
</html>
Search WWH ::




Custom Search