O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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).

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required