11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

var pattern = /ab+c/;<br />

is read as ―a‖ followed by ―b‖ repeated one or more times, followed by ―c.‖ Keeping this pattern<br />

in mind, you can see that it matches all the following strings:<br />

abc<br />

abbbbbc<br />

<strong>The</strong> letters abc begin the alphabet<br />

Conversely, the pattern does not match the string ―ac‖ because it does not contain at least one<br />

―b‖ between ―a‖ and ―c.‖<br />

<strong>The</strong> ? quantifier indicates that the previous item may occur zero times or one time, but no more.<br />

For example:<br />

var pattern = /ab?c/;<br />

Read this pattern as ―a‖ followed by zero or one ―b‖s followed by ―c.‖ It matches ―ac‖ and ―abc,‖<br />

but not ―abbc.‖ <strong>The</strong> ? essentially denotes that the preceding item is optional.<br />

<strong>The</strong> repetition quantifiers so far haven‘t provided any way to specify that a particular character<br />

is to be repeated some exact number of times. Curly braces ({ }) are used to indicate the<br />

number of repetitions allowed for the preceding token (character). For example,<br />

var pattern = /ab{5}c/;<br />

specifies a pattern consisting of an ―a‖ followed by exactly five ―b‖ characters and then the letter<br />

―c.‖ Of course, this particular expression could have also been written as<br />

var pattern = /abbbbbc/;<br />

But this ―long‖ version would be very cumbersome if you wanted to match, say, a character<br />

repeated 25 times.<br />

Using the curly braces it is possible to precisely indicate that the number of repetitions falls<br />

within a specific range. To do so, list inside the curly braces the fewest number of repetitions<br />

allowed followed by a comma and the maximum allowed. For example,<br />

var pattern = /ab{5,7}c/;<br />

creates a regular expression matching a single ―a‖ followed by between five and seven<br />

(inclusive) ―b‖ characters and then the letter ―c.‖<br />

Omitting the maximum amount from within the curly braces (but still including the comma)<br />

specifies a minimum number of repetitions. For example,<br />

var pattern = /ab{3,}c/;<br />

creates an expression matching an ―a‖ followed by three or more letter ―b‖ characters followed<br />

by a ―c.‖<br />

<strong>The</strong> full list of repetition quantifiers is summarized in Table 8-3.<br />

Table 8-3: Repetition Quantifiers<br />

Character Meaning<br />

>* Match previous item zero or more times.<br />

>+ Match previous item one time or more.<br />

>? Match previous item zero or one times.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!