ActionScript 3.0 has regular expressions built in, as defined by ECMAScript edition 3
(ECMA-262), with a few additions: the s and x flags and named
groups. A regular expression is an object of class RegExp.
Creating a regular expression
/pattern/flags
new RegExp(pattern:String, flags:String)
The literal form is written straight into the code. The constructor builds the
expression from strings at run time; because the pattern is a string literal there,
each backslash must be doubled, and a / needs no escape.
var date1:RegExp = /(\d{4})-(\d{2})-(\d{2})/;
var date2:RegExp = new RegExp("(\\d{4})-(\\d{2})-(\\d{2})");
var path:RegExp = /\/images\/[^\/]+\.png$/i;
Flags
| Flag | Property | Meaning |
|---|---|---|
g | global | Find every match: replace replaces all, and exec and test carry on from lastIndex. |
i | ignoreCase | Ignore upper and lower case. |
m | multiline | ^ and $ also match at the start and end of each line. |
s | dotall | . also matches a line break. Not in ECMAScript 3. |
x | extended | White space in the pattern is ignored, so it can be laid out for reading. Not in ECMAScript 3. |
The flag properties are read-only. source gives the pattern text, and
lastIndex is the position where the next global search starts.
Metacharacters
These have a special meaning; a backslash before one matches it literally:
\. matches a full stop.
| Character | Meaning |
|---|---|
^ | The start of the string, or of a line with m. First in a character class, it negates the class. |
$ | The end of the string, or of a line with m. |
\ | Escapes the next character, or starts a metasequence. |
. | Any single character except a line break (any at all with s). |
*, +, ? | Zero or more, one or more, zero or one of the item before. |
( ) | A group. |
[ ] | A character class. |
{ } | A counted quantifier. |
| | Either the part on the left or the part on the right. |
Metasequences
| Sequence | Matches |
|---|---|
\d, \D | A decimal digit; anything but a digit. |
\w, \W | A word character (ASCII letter, digit or _); anything else. |
\s, \S | A white space character; anything else. |
\b, \B | A word boundary; a position that is not one. |
\n, \r, \t, \f, \v | Line feed, carriage return, tab, form feed, vertical tab. |
\unnnn | The character with hexadecimal code nnnn. |
\xnn | The character with hexadecimal code nn. |
\1 … \99 | The text an earlier capturing group matched. |
Character classes
| Class | Matches |
|---|---|
[abc] | Any one of a, b or c. |
[a-z0-9] | A range: any lower-case ASCII letter or digit. |
[^abc] | Any character except those listed. |
[\d.\-] | Metasequences work inside; most metacharacters lose their meaning there. A - is literal first or last, or escaped; ], \ and a leading ^ need escaping. |
Quantifiers
| Quantifier | Repeats the item before |
|---|---|
* | Zero or more times. |
+ | One or more times. |
? | Zero or one time. |
{n} | Exactly n times. |
{n,} | At least n times. |
{n,m} | From n to m times. |
*?, +?, ??, {n,m}? | The same, but lazy: as few times as will still match. |
Quantifiers are greedy by default: /<.*>/ on
<b>x</b> matches the whole string, and
/<.*?>/ only <b>.
Groups
| Group | Meaning |
|---|---|
( … ) | A capturing group: groups the items for a quantifier or |, and keeps the text it matched for \1 to \99, $1 to $99 and the exec result. |
(?: … ) | A group that does not capture. |
(?= … ) | Positive lookahead: what follows must match, without being consumed. /sh(?=e)(\w*)/i matches Shelly but not shore. |
(?! … ) | Negative lookahead: what follows must not match. |
(?P<name> … ) | A named group, an ActionScript 3.0 addition: the match is also a property of the exec result under that name. |
var re:RegExp = /(?P<user>[\w.\-]+)@(?P<domain>[\w\-]+)\.\w+/;
var result:Object = re.exec("Write to bob@example.com today");
trace(result[0]); // bob@example.com
trace(result.user); // bob
trace(result.domain); // example
trace(result.index); // 9
exec result.RegExp methods
| Method | Gives |
|---|---|
re.exec(str) | null when there is no match; otherwise an array of the whole match and each group, with index (where it starts), input (the string) and any named groups. With g, it starts at lastIndex and moves lastIndex past the match, so calling it in a loop finds each match in turn. |
re.test(str) | true or false, following the same lastIndex rule. |
String methods that take a regular expression
| Method | Gives |
|---|---|
str.match(re) | Without g, the same array as exec; with g, an array of every match; null when none. |
str.search(re) | The index of the first match, or −1. g and lastIndex are ignored. |
str.replace(re, replacement) | A new string with the first match, or every match with g, replaced by a string or by the result of a function. |
str.split(re, limit) | The pieces between matches, as an array. |
In a replacement string these codes are filled in:
| Code | Inserts |
|---|---|
$& | The matched text. |
$` | The text before the match. |
$' | The text after the match. |
$1 … $9, $01 … $99 | The text of that group; empty if it did not take part. |
$$ | A dollar sign. |
A replacement function is called for each match with the matched text, then one argument per group, then the index of the match and the whole string; it returns the replacement.
var s:String = "2026-11-07";
trace(s.replace(/(\d+)-(\d+)-(\d+)/, "$3/$2/$1")); // 07/11/2026
var shout:String = "one two".replace(/\b\w/g,
function (m:String, index:int, all:String):String {
return m.toUpperCase();
}); // One Two