9.6. Matching Patterns with Regular Expressions
Problem
You want to match a pattern within a string instead of finding a specific substring.
Solution
Use a regular expression
and the RegExp.exec( )
method.
Discussion
Many 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.
Table 9-2. Regular expressions
Expression |
Matches |
Example |
---|---|---|
? |
The preceding character zero or one time (i.e., preceding character is optional). |
|
* |
The preceding character zero or more times. |
|
+ |
The preceding character one or more times. |
|
. (period) |
Any one character except newline. |
|
^ |
Specified string at beginning of a line. |
|
Get Actionscript Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.