HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [179]
If you forget and enclose a regular expression inside quotes, it will still work most of the time. JavaScript tries to convert string values into regular expressions when it needs to. However, if you’ve ever watched a science fiction movie, you know it’s generally not best to trust computers. Use the slash characters to explicitly coerce the text into regular expression format. I’m not saying your computer will take over the world if you don’t, but you never can tell. . . .
This match is the simplest type. I’m simply looking for the existence of the needle (bigCorp) in a haystack (the e-mail address stored in email). If bigCorp is found anywhere in the text, the match is true, and I can do what I want (usually process the form on the server). More often, you want to trap for an error and remind the user what needs to be fixed.
Marking the beginning and end of the line
You may want to improve the search, because what you really want are addresses that end with bigCorp.com. You can put a special character inside the match string to indicate where the end of the line should be:
if (email.match(/bigCorp.com$/)){
alert(“match”);
} else {
alert(“no match”);
} // end if
The dollar sign at the end of the match string indicates that this part of the text should occur at the end of the search string, so andy@bigCorp.com is a match, but not bigCorp.com announces a new Website.
If you’re an ace with regular expressions, you know this example has a minor problem, but it’s pretty picky. I explain it in the upcoming “Working with special characters” section. For now, just appreciate that you can include the end of the string as a search parameter.
Likewise, you can use the caret character (^) to indicate the beginning of a string.
If you want to ensure that a text field contains only the phrase oogie boogie (and why wouldn’t you?), you can tack on the beginning and ending markers. The code /^oogie boogie$/ is a true match only if nothing else appears in the phrase.
Working with special characters
In addition to ordinary text, you can use a bunch of special character symbols for more flexible matching:
♦ Matching a character with the period: The most powerful character is the period (.), which represents a single character. Any single character except the newline (\n) matches against the period. A character that matches any character may seem silly, but it’s actually quite powerful. The expression /b.g/ matches big, bag, and bug. In fact, it matches any phrase that contains b followed by any single character and then g, so bxg, b g, and b9g are also matches.
♦ Using a character class: You can specify a list of characters in square braces, and JavaScript matches if any one of those characters matches. This list of characters is sometimes called a character class. For example, /b[aeiou]g/ matches on bag, beg, big, bog, or bug. This method is a really quick way to check a lot of potential matches.
You can also specify a character class with a range. [a-zA-Z] checks all the letters.
♦ Specifying digits: One of the most common tricks is to look for numbers. The special character \d represents a number (0–9). You can check for a U.S. phone number (without the area code — yet) using a pattern that looks for three digits, a dash, and four digits: /\d\d\d-\d\d\d\d/.
♦ Marking punctuation characters: You can tell that regular expressions use a lot of funky characters, such as periods and braces. What if you’re searching for one of these characters? Just use a backslash to indicate that you’re looking for the actual character and not using it as a modifier. For example, the e-mail address would be better searched with bigCorp\.com because it specifies there must be a period. If you don’t use the backslash, the regular expression tool interprets the period as “any character” and allows something like bigCorpucom. Use the backslash trick for most punctuation, such as parentheses, braces, periods, and slashes.