9.6. Matching Patterns with Regular Expressions


You want to match a pattern within a string instead of finding a specific substring.


Use a regular expression and the RegExp.exec( ) method.


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





The preceding character zero or one time (i.e., preceding character is optional).

ta?k matches “tak” or “tk” but not “tik” or “taak”


The preceding character zero or more times.

wo*k matches “wok”, “wk”, or “woook”, but not “wak”


The preceding character one or more times.

craw+l matches “crawl” or “crawwl” but not “cral”

. (period)

Any one character except newline.

c.ow matches “crow” or “clow” but not “cow”


Specified string at beginning of a line.

^wap ...

Get Actionscript Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.