HTML and CSS Reference
In-Depth Information
EXPLANATION
1
A new array object is created.
2
The string “apples pears,peaches:plums,oranges” is assigned to the variable called
myString . The delimiters are a tab, comma, and colon.
3
The regular expression /[\t:,]/ is assigned to the variable called regex .
4
The String object's split() method splits up the string using a tab, colon, or comma
as the delimiter. The delimiting characters are enclosed in square brackets, which
in regular expression parlance is called a character class. (See the section “Getting
Control—The Metacharacters” on page 733.) In simple terms, any one of the
characters listed within the brackets is a delimiter in the string. The split() method
will search for any one of these characters and split the string accordingly, return-
ing an array called splitArray .
5
Each of the array elements is displayed in the page. See Figure 17.10.
Figure 17.10 The string is split on tabs, colons, and commas.
17.4 Getting Control—The Metacharacters
Regular expression metacharacters are characters that do not represent themselves.
They are endowed with special powers to allow you to control the search pattern in some
way (e.g., find the pattern only at the beginning of line, or at the end of the line, or if it
starts with an upper- or lowercase letter, etc.). Metacharacters will lose their special
meaning if preceded with a backslash. For example, the dot metacharacter represents
any single character, but when preceded with a backslash it is just a dot or period.
If you see a backslash preceding a metacharacter, the backslash turns off the meaning
of the metacharacter, but if you see a backslash preceding an alphanumeric character in
a regular expression, then the backslash is used to create a metasymbol. A metasymbol
provides a simpler form to represent some of the regular expression metachacters. For
example, [0-9] represents numbers in the range between 0 and 9, and \d , the metasym-
bol, represents the same thing. [0-9] uses the bracketed character class, whereas \d is a
metasymbol (see Table 17.6).
EXAMPLE 17.10
/^a...c/
 
 
Search WWH ::




Custom Search