[ Team LiB ] |
Recipe 9.6 Matching Patterns with Regular Expressions9.6.1 ProblemYou want to match a pattern within a string instead of finding a specific substring. 9.6.2 SolutionUse a regular expression and the RegExp.exec( ) method. 9.6.3 DiscussionMany programming languages support regular expressions to match patterns in strings. (You may be familiar with other types of pattern matching. For example, Windows' file search feature lets you use pattern matching with wildcards, such as * and ?. But regular expressions support much more sophisticated pattern matching.) ActionScript does not provide native support for regular expressions. However, there are several third-party classes that are publicly available. One such class is the RegExp class by Pavils Jurjans, which is very similar to the JavaScript 1.3 RegExp class (JavaScript 1.5 implements new RegExp features not supported by the ActionScript RegExp class). Table 9-2 summarizes regular expression pattern-matching operations.
The RegExp class does not try to interpret escape sequences that are natively interpreted by Flash. The ActionScript RegExp class interprets the escape sequences \d, \D, \s, \S, \w, \W, \b, and \B, but other escape sequences, such as \n and \t, are interpreted by Flash itself. Therefore, if you want to match a newline character, you should use the pattern "\n", but if you want to match a digit, you use the pattern "\\d" (note the double backslash before "d"). You must first download and install the RegExp class if you wish to use it in your Flash documents. You can download it from Pavils's web site: You can download the ActionScript file itself (RegExp.as) or a zip file (RegExp.zip) that contains additional support files. Whichever download you choose, copy the RegExp.as file into your Flash installation's Include directory (Flash Installation/Configuration/Include). From there, you can easily include it in any Flash document. The main difference between Pavils's ActionScript RegExp class and its JavaScript kin is that there is no way to define a regular expression using an object initializer. Instead, you must always use the constructor method: // This will work in JavaScript but not in ActionScript. re = /[a-z]*/; // This is the proper way to create a regular expression in ActionScript. re = new RegExp("[a-z]*"); // Create a regular expression that matches a backslash. re = new RegExp("\\\\");
When constructing a regular expression, you can specify a second parameter containing flags that modify its behavior. The most common flags are "i" for case-insensitive matches and "g" for global matching (finds all matches at once and returns them in an array). For example: // This matches all letters a, b, c, A, B, and C. re = new RegExp("[a-c]", "ig"); Once you have created a regular expression that describes the pattern for which you want to search within the string, use the regExp.exec( ) method to perform the search. The exec( ) method takes the string as a parameter, and it returns the match. Each call to exec( ) searches for the next match. If no match is found, it returns null. // You must include the third-party RegExp.as file from // http://www.jurjans.lv/flash/RegExp.html. #include "RegExp.as" // Create a regular expression that matches three-letter words. re = new RegExp("\\b[a-z]{3}\\b", "g"); myString = "This string has two three-letter words"; // Search the string for the pattern and display the first result: has. match = re.exec(myString); trace(match); // Search the string again for the pattern and display the next result: two. match = re.exec(myString); trace(match); // Search the string again. No more matches, so the result is null. match = re.exec(myString); trace(match);
You can use a while statement with the exec( ) method to find all the matches, like so: #include "RegExp.as" // Create a regular expression that matches three-letter words. re = new RegExp("\\b[a-z]{3}\\b", "g"); myString = "This string has two three-letter words"; /* Loop until the exec( ) method returns null. This while loop outputs: has two */ while ((match = re.exec(myString)) != null) { trace(match); } The RegExp.test( ) method tests whether a string contains a match to a regular expression. The method returns true if the pattern is matched, and false otherwise. You can use test( ) to test whether a string is valid for a particular use, such as whether it takes the form of a valid email address. For example: #include "RegExp.as" // Create a regular expression that matches an email pattern. re = new RegExp("^([\\w\-\\.]+)@(([\\w\\-]+\\.)+[\\w\\-]+)$"); // Create an array of strings that may or may not be valid emails. emails = new Array( ); emails.push("someone@someserver.com"); emails.push("your.name@someplace.org"); emails.push("email goes here"); /* Test each array element to see whether it is a valid email. The results are: true true false */ for (var i = 0; i < emails.length; i++) { trace(re.test(emails[i])); } 9.6.4 See AlsoA detailed discussion of regular expressions is beyond the scope of this book. A good primer on the JavaScript RegExp class can be found at http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/regexp.html. JavaScript: The Definitive Guide by David Flanagan (O'Reilly) includes detailed coverage of using regular expressions in JavaScript. See Mastering Regular Expressions by Jeffrey E. F. Friedl (O'Reilly) for extensive practice with regular expressions. Also refer to Recipe 9.4 and Recipe 9.7. Recipe 9.9 demonstrates using regular expressions to remove nonalphanumeric characters in a string. Also see Recipe 8.7, which covers filtering text input. Table A-1 lists the Unicode code points for the Latin 1 character set. Recipe 11.4 discusses validating data input. |
[ Team LiB ] |