HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [180]
If you want to include an area code with parentheses, just use back-slashes to indicate the parentheses: /\(\d\d\d\) \d\d\d-\d\d\d\d/. And if you want to ensure the only thing in the sample is the phone number, just add the boundary characters: /^\(\d\d\d\) \d\d\d \d\d\d\d$/.
♦ Finding word boundaries: Sometimes you want to know whether something is a word. Say that you’re searching for the, but you don’t want a false positive on breathe or theater. The \b character means “the edge of a word,” so /\bthe\b/ matches the but not words containing “the” inside them.
Conducting repetition operations
All the character modifiers refer to one particular character at a time, but sometimes you want to deal with several characters at once. Several operators can help you with this process.
♦ Finding one or more elements: The plus sign (+) indicates “one or more” of the preceding character, so the pattern /ab+c/ matches on abc, abbbbbbc, or abbbbbbbc, but not on ac (there must be at least one b) or on afc (it’s gotta be b).
♦ Matching zero or more elements: The asterisk means “zero or more” of the preceding character. So /I’m .* happy/ matches on I’m happy (zero occurrences of any character between I’m and happy). It also matches on I’m not happy (because characters appear in between).
The .* combination is especially useful, because you can use it to improve matches like e-mail addresses: /^.*@bigCorp\.com$/ does a pretty good job of matching e-mail addresses in a fictional company.
♦ Specifying the number of matches: You can use braces ({}) to indicate the specific number of times the preceding character should be repeated. For example, you can rewrite a phone number pattern as /\(\d{3}\) *\d{3}-\d{4}/. This structure means “three digits in parentheses, followed by any number of spaces (zero or more), and then three digits, a dash, and four digits. Using this pattern, you can tell whether the user has entered the phone number in a valid format.
You can also specify a minimum and maximum number of matches, so /[aeiou]{1, 3}/ means “at least one and no more than three vowels.”
Now you can improve the e-mail pattern so that it includes any number of characters, an @ sign, and ends with a period and two to four letters: /^.+@.+\..{2,4}$/.
Working with pattern memory
Sometimes you want to remember a piece of your pattern and reuse it. You can use parentheses to group a chunk of the pattern and remember it. For example, /(foo){2}/ doesn’t match on foo, but it does on foofoo. It’s the entire segment that’s repeated twice.
You can also refer to a stored pattern later in the expression. The pattern /^(.).*\1$/ matches any word or phrase that begins and ends with the same character. The \1 symbol represents the first pattern in the string; \2 represents the second, and so on.
After you’ve finished a pattern match, the remembered patterns are still available in special variables. The variable $1 is the first; $2 is the second, and so on. You can use this trick to look for HTML tags and report what tag was found: Match ^<(.*)>.*<\/\1>$ and then print $1 to see what the tag was.
There’s much more to discover about regular expressions, but this basic overview should give you enough to write some powerful and useful patterns.
Chapter 7: Animating Your Pages
In This Chapter
Moving an object on the screen
Responding to keyboard input
Reading mouse input
Running code repeatedly
Bouncing off the walls
Swapping images
Preloading image files
Reusing code
Using external script files
JavaScript has a serious side, but it can be a lot of fun, too. You can easily use JavaScript to make things move, animate, and wiggle. In this chapter, you find out how to make your pages dance. Even if you aren’t interested in animation, you can discover important ideas about how to design your pages and code more efficiently.
Making Things Move
You may think you need Flash or Java to put animation in your pages, but that’s not the only way. You can use JavaScript to create some pretty