14.08.2016 Views

Beginning JavaScript with DOM Scripting and Ajax, 2nd Edition

Create successful ePaper yourself

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

Chapter 9 ■ Data ValiDation teChniques<br />

administrators, you’ll be amazed how often they can replace a 50-line switch/case or if/else construct you wrote in<br />

<strong>JavaScript</strong> <strong>with</strong> a single regular expression. Many editing environments also feature “find” <strong>and</strong> “search <strong>and</strong> replace”<br />

functionality, allowing the use of regular expressions.<br />

Regular expressions are the cat’s pajamas once you get your head around them; however, at first sight a construct<br />

like /^[\w]+(\.[\w]+)*@([\w]+\.)+[a-z]{2,7}$/i (which checks whether a string is a valid e-mail syntax) can<br />

strike fear into the faint of heart. The good news is that it is not as tough as it looks.<br />

Syntax <strong>and</strong> Attributes<br />

Imagine you want to search for the string cat in text. You can define this as a regular expression in two different<br />

formats:<br />

// Expression literals; notice that you must not use quotation marks!<br />

var searchTerm = /cat/;<br />

// Object constructor<br />

var searchTerm = new RegExp('cat');<br />

If you use this expression on a string via the match(), search(), exec(), or test() method, it’ll return anything<br />

that has “cat” in it—regardless of the location in the string—like catalog, concatenation, or scat.<br />

If you want to match only the word “cat” as a string <strong>with</strong>out anything else around it, you need to use a ^ to indicate<br />

the start <strong>and</strong> a $ for the end:<br />

var searchTerm = /^cat$/;<br />

var searchTerm = new RegExp('^cat$');<br />

You can also omit either the start indicator, ^, or the end indicator, $. This would match cat, catalog, or catastrophe:<br />

var searchTerm = /^cat/;<br />

var searchTerm = new RegExp('^cat');<br />

The following code would find polecat or wildcat:<br />

var searchTerm = /cat$/;<br />

var searchTerm = new RegExp('cat$');<br />

If you want to find “cat” regardless of case—for example, to match cat, Catherine, or CAT—you need to use the<br />

i attribute following the second slash. This causes the case to be ignored:<br />

var searchTerm=/cat/i;<br />

var searchTerm=new RegExp('cat', 'i');<br />

If you have a string that could have the word “cat” in it several times, <strong>and</strong> you want to get all matches as an array,<br />

you need to add the parameter g for global:<br />

var searchTerm = /cat/g;<br />

var searchTerm = new RegExp('cat', 'g');<br />

By default, regular expressions match patterns only in single-line strings. If you want to match a pattern in a<br />

multiline string, use the parameter m for multiline. You can also mix them, <strong>and</strong> the order is not important:<br />

var searchTerm = /cat/gim;<br />

var searchTerm = new RegExp('cat', 'mig');<br />

www.it-ebooks.info<br />

281

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

Saved successfully!

Ooh no, something went wrong!