02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Using Character Class Shorth<strong>and</strong><br />

CHAPTER 9 ■ PERFORMING FORM VALIDATION WITH REGULAR EXPRESSIONS<br />

Certain character classes have a shorth<strong>and</strong> character. For example, there is a shorth<strong>and</strong> class for every<br />

word, digit, or space character:<br />

• Word character class shorth<strong>and</strong> (\w): Matches patterns like [A-Za-z0-9_]<br />

• Digit character class shorth<strong>and</strong> (\d): Matches patterns like [0-9]<br />

• Whitespace character class shorth<strong>and</strong> (\s): Matches patterns like [ \t\r\n]<br />

Using these three shorth<strong>and</strong> classes can improve the readability of your regexes, which is extremely<br />

convenient when you’re dealing with more complex patterns.<br />

You can exclude a particular type of character <strong>by</strong> capitalizing the shorth<strong>and</strong> character:<br />

• Non-word character class shorth<strong>and</strong> (\W): Matches patterns like [^A-Za-z0-9_]<br />

• Non-digit character class shorth<strong>and</strong> (\D): Matches patterns like [^0-9]<br />

• Non-whitespace character class shorth<strong>and</strong> (\S): Matches patterns like [^ \t\r\n]<br />

■ Note \t, \r, <strong>and</strong> \n are special characters that represent tabs <strong>and</strong> newlines; a space is represented <strong>by</strong> a<br />

regular space character ( ).<br />

Finding Word Boundaries<br />

Another special symbol to be aware of is the word boundary symbol (\b). By placing this before <strong>and</strong>/or<br />

after a pattern, you can ensure that the pattern isn’t contained within another word. For instance, if you<br />

want to match the word stat, but not thermostat, statistic, or ecstatic, you would use this pattern:<br />

/\bstat\b/.<br />

Using Repetition Operators<br />

When you use character classes, only one character out of the set is matched, unless the pattern specifies<br />

a different number of characters. Regular expressions give you several ways to specify a number of<br />

characters to match:<br />

• The star operator (*) matches zero or more occurrences of a character.<br />

• The plus operator (+) matches one or more occurrences of a character.<br />

• The special repetition operator ({min,max}) allows you to specify a range of<br />

character matches.<br />

Matching zero or more characters is useful when using a string that may or may not have a certain<br />

piece of a pattern in it. For example, if you want to match all occurrences of either John or John Doe, you<br />

can use this pattern to match both instances: /John( Doe)*/.<br />

323

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

Saved successfully!

Ooh no, something went wrong!