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. |
|
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access