HTML and CSS Reference
In-Depth Information
EXPLANATION ( CONTINUED )
3
The String object's replace() method will search for the pattern, regex , in the string
and if it finds the pattern will replace it with Mom . If the g modifier were used, all
occurrences of the pattern would be replaced with Mom (see Figure 17.8). For ex-
ample, /tom/ig would result in “Mommy has a sMomach ache.”
Figure 17.8 The first occurrence of Tom , uppercase or lowercase, is replaced with
Mom.
17.3.4 The split() Method
The String object's split() method splits a single text string into an array of substrings.
In a real-world scenario, it would be like putting little crayon marks at intervals on a
piece of string and then cutting the string everywhere a mark appeared, thus ending up
with a bunch of little strings. In the JavaScript world, the crayon mark is called a delim-
iter , which is a character or pattern of characters that marks where the string is to be
split up. When using the String object's split() method, if the words in a string are sepa-
rated by commas, then the comma would be the delimiter and if the words are separated
by colons, then the colon is the delimiter. The delimiter can contain more complex com-
binations of characters if regular expression metacharacters are used.
FORMAT
array = String.split( /delimiter/ );
EXAMPLE
splitArray = "red#green#yellow#blue".split(/#/);
(splitArray is an array of colors. splitArray[0] is "red")
EXAMPLE 17.8
<html>
<head><title>The split() Method</title></head>
<body>
<script type = "text/javascript">
1
var splitArray = new Array();
2
var string="apples:pears:peaches:plums:oranges";
3
var regex = /:/;
Continues
 
 
Search WWH ::




Custom Search