HTML and CSS Reference
In-Depth Information
Figure 17.19 The user entered ABCD# . To match, the string needs a space
between the C and D .
17.4.4 Metacharacters to Repeat Pattern Matches
In the previous examples, the metacharacter matched on a single character. What if you
want to match on more than one character? For example, let's say you are looking for all
lines containing names and the first letter must be in uppercase, which can be repre-
sented as [A-Z] , but the following letters are lowercase and the number of letters varies
in each name. [a-z] matches on a single lowercase letter. How can you match on one or
more lowercase letters, or zero or more lowercase letters? To do this you can use what
are called quantifiers . To match on one or more lowercase letters, the regular expression
can be written /[a-z]+/ where the + sign means “one or more of the previous characters”;
in this case, one or more lowercase letters. JavaScript provides a number of quantifiers
as shown in the Table 17.9.
Table 17.9 Quantifiers: The Greedy Metacharacters
Metacharacter
What It Matches
x?
Matches 0 or 1 of x.
(xyz)?
Matches zero or one pattern of xyz.
x*
Matches 0 or more of x.
(xyz)*
Matches zero or more patterns of xyz.
x+
Matches 1 or more of x.
(xyz)+
Matches one or more patterns of xyz.
x{m,n}
Matches at least m of x and no more than n of x.
The Greed Factor. Normally quantifiers are “greedy”; that is, they match on the
largest possible set of characters starting at the left of the string and searching to the
right, looking for the last possible character that would satisfy the condition. For exam-
ple, given the string:
var string="ab123456783445554437AB"
 
 
Search WWH ::




Custom Search