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.

would match ―abcx7d‖ or ―abc_-d.‖ Other common classes are \s, any whitespace character; \S,<br />

any non-whitespace character; \w, any word character; \W, any non-word character; \d, any<br />

digit; and \D, any non-digit. (Notice the pattern: the uppercase version of shorthand is the<br />

opposite of the lowercase). <strong>The</strong> complete list of character classes is given in Table 8-4.<br />

Table 8-4: Regular Expression Character Classes<br />

Character Meaning<br />

>[chars] Any one character indicated either explicitly or as a range between the<br />

brackets.<br />

>[^chars] Any one character not between the brackets represented explicitly or as<br />

a range.<br />

>. Any character except newline.<br />

>\w Any word character. Same as [a-zA-Z0-9_].<br />

>\W Any non-word character. Same as [^a-zA-Z0-9_].<br />

>\s Any whitespace character. Same as [ \t\n\r\f\v].<br />

>\S Any non-whitespace character. Same as [^ \t\n\r\f\v].<br />

>\d Any digit. Same as [0-9].<br />

>\D Any non-digit. Same as [^0-9].<br />

>\b A word boundary. <strong>The</strong> empty ―space‖ between a \w and \W.<br />

>\B A word non-boundary. <strong>The</strong> empty ―space‖ between word characters.<br />

>[\b] A backspace character.<br />

We can use these shorthands to write an even more concise version of our isPhoneNumber()<br />

function:<br />

function isPhoneNumber(phone) { var pattern = /^\d{3}-\d{3}-\d{4}$/; return pattern.test(phone);}<br />

We‘ve replaced each [0-9] character class with its shorthand, \d.<br />

Alternatives<br />

<strong>The</strong> final major tool necessary to define useful patterns is |, which indicates the logical OR of<br />

several items. For example, to match a string that begins with ―ftp,‖ ―http,‖ or ―https,‖ you might<br />

write<br />

var pattern = /^(http|ftp|https)/;<br />

Unlike repetition quantifiers that only apply to the previous item, alternation separates complete<br />

patterns. If we had written the preceding example as<br />

var pattern = /^http|ftp|https/;<br />

the pattern would have matched a string beginning with ―http‖ or a string containing ―ftp‖ or a<br />

string containing ―https.‖ <strong>The</strong> initial ^ would‘ve been included only in the first alternative pattern.<br />

To further illustrate, consider the following regexp:<br />

var pattern = /James|Jim|Charlie Brown/;<br />

Since each | indicates a new pattern, this matches a string containing ―James,‖ a string<br />

containing ―Jim,‖ or a string containing ―Charlie Brown.‖ It does not match a string containing<br />

―James Brown‖ as you might have thought. Parenthesizing alternatives limits the effect of the |<br />

to the parenthesized items, so you see the following pattern,

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

Saved successfully!

Ooh no, something went wrong!