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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Figure 9-8. Using alternation to match only three-, six-, <strong>and</strong> seven-letter words<br />

Using Optional Items<br />

In some cases, it becomes necessary to allow certain items to be optional. For instance, to match both<br />

single <strong>and</strong> plural forms of a word like expression, you need to make the s optional.<br />

To do this, place a question mark (?) after the optional item. If the optional part of the pattern is<br />

longer than one character, it needs to be captured in a group (you’ll use this technique in the next<br />

section).<br />

For now, use this pattern to highlight all occurrences of the word expression or expressions:<br />

/(expressions?)/i (see Figure 9-9).<br />

Figure 9-9. Matching a pattern with an optional s at the end<br />

325

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

Saved successfully!

Ooh no, something went wrong!