HTML and CSS Reference
In-Depth Information
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
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.
array = String.split( /delimiter/ );
splitArray = "red#green#yellow#blue".split(/#/);
(splitArray is an array of colors. splitArray[0] is "red")
<head><title>The split() Method</title></head>
<script type = "text/javascript">
var splitArray = new Array();
var string="apples:pears:peaches:plums:oranges";
var regex = /:/;
Search WWH ::

Custom Search