Testing Regular Expressions in ActionScript 3

I’m by no means an expert on Regular Expressions. I mean, I get what they do, and can see their awesomeness, but damn they do my head in sometimes.
To help myself out, I wrote a quick little Flex app that lets you test how a regular expression will work with a particular string. It lets you try out all the Flash Regular Expression methods and see each of the results.
Unfortunately it was written in flash so doesn’t really work anymore:

I did put together a quick overview of the different Flash Regular Expression methods that are available, as well as a short overview of the Regular Expression syntax.
Hope that helps you out!
P.S. If you are looking for testing out reular expressions, I have found https://regex101.com/ to be pretty great.
Regular Expression Methods
Here are the Regular Expression methods that are used in AS3
RegExp.test(string:String):Boolean
Simply tests to see if the pattern is matched anywhere in the string
- if ‘g’ flag is set it searches from the .lastIndex, and also sets it after each .test()
- if ‘g’ is not set, then the search is always done from the start
ActionScript Language Reference — RegExp.test() (archived) for the official documentation
RegExp.exec(string:String):Object
Returns an object with with various properties relating to the pattern. The properties are
- \,\ etc - Array like access - \
- index - position of the match
- input - the string that was tested
If there is no match then this will return null.
- if the ‘g’ flag is set, it can be called multiple times
- if the ‘g’ flag is not set, it will only return the first match
ActionScript Language Reference — RegExp.exec() (archived) for the official documentation
String.replace(pattern:RegExp, replacement:String):Boolean
Replaces the pattern in the string with the replacement string.
- if ‘g’ flag is set it replaces all matches in the string
- if ‘g’ is not set,only the first match is replaced
- you can use group references like $1, $2, $`, $&, $’
ActionScript Language Reference — String.replace() (archived) for the official documentation
String.search(pattern:RegExp):int
Returns the index of where the pattern is found in the string.
- returns -1 if there is no match
- ignores the ‘g’ flag and ‘.lastIndex’ property
ActionScript Language Reference — String.search() (archived) for the official documentation
String.match(pattern:RegExp):Array
Returns an Array populated with values relating to the pattern.
- if the ‘g’ flag is not set, the array will be in the form of \
- if the ‘g’ flag is set, the array will contain all the possible full pattern matches - \
ActionScript Language Reference — String.match() (archived) for the official documentation
Regular Expression Syntax
Here is a quick run-through of the regular expression syntax as a bit of reference.
Constructors
There are two ways to set up a regular expression.
| Method | Description |
|---|---|
| /pattern/flags | Literal: Just type the shorthand method, and bam, regular expression |
| new RegExp(pattern:String, flags:String) | Constructor: Construct it like any type of object, passing in a string of the pattern and the flags as parameters. Note: If using RegExp then you need to escape sequences - \\d instead of the normal \d |
Flags
Flags change the way the regular expression works. They also can effect the results of the regular expression methods in different ways.
| Flag | Reason |
|---|---|
| g | Global |
| i | Case-insensitive |
| m | Multi-line |
| s | Dot all |
| x | Extended |
Meta Characters
Meta Characters are characters that have a special meaning when used inside a Regular Expression pattern.
| Character | Description |
|---|---|
| ^ (caret) | Matches the start of a string If using ‘multiline’ (m) then it will match the start of a line. |
| $ | Matches the end of a string If using multiline (m) then it will match the end of a line. |
| \ | Escapes special characters. \d, \w, \\, etc |
| . (period) | Matches any single character. If using the ‘dot all’ (s) flag this will match new lines as well. |
| * | Matches the previous character 0 or more times. |
| + | Matches the previous character 1 or more times. |
| ? | Matches previous character 0 or 1 time |
| ( ) | Groups - define a scope of a quantifier (walla){1,2} - confine scope of the | alternator - used to define back references |
| [ ] | Character Classes - Defines possible for matches for a single character - You can use hyphens to define a range - \ - Backslash (\) escapes ] and - - ^ in first position negates set - note - meta characters are treated as normal characters and need to be escaped to work - [\\d] |
| | | Alternation / Logical Or - allows you to test between multiple patters (abc|xyz) matches abc or xyz /1|2|3|4|5/ equals \ |
Meta Sequences
Meta Sequences are special string that can be used to represent specific characters in a pattern.
| Sequence | Description |
|---|---|
{n} {n,} {n,m} | Numeric quantifier - A{27} = A 27 times - A{3,} = A 3 or more times - A{3,5} = A 3 to 5 times |
| \b | Matches the position between a word character and a non-word character. If first or last character in the string is a word character, also matches the start/end. |
| \B | Matches the position between 2 word characters. Also matches the position between 2 non word characters. |
| \d \D | Decimals Non-decimals |
| \f \n \r \v | Form feed Newline Return Vertical feed |
| \s \S | Whitespace - space, tab, newline, return Non-whitespace |
| \t | Tab character |
| \w \W | Word character - A-Z, a-z, 0-9,_ Non-word character |
| \unnnn | Matches unicode nnnn |
| \xnn | ascii character |
Greedy Vs Non-Greedy
Normally quantifiers like * and + are greedy, as in, they will try and match as much as they can. They can be made lazy by adding a ? afterward.
| Quantifier | Description |
|---|---|
| / .*/ / | greedy - the * will match as many characters as possible |
| / .*?/ / | lazy - the * will match the least amount of characters |
Capturing sub-strings
Sometimes when you are looking for a pattern in a string you need to work with a sub-string. Maybe it is to replace it, or reference it is a new string. Maybe you don’t know what exactly it is you have to match. That’s where this stuff comes in.
| Substring | Description |
|---|---|
| \1, \2, etc | Back reference Matches an earlier defined group in the pattern eg: /(\d+)-by-\1/ matches 48-by-48 |
| $1 - $nn | Group references Can be used to reference groups in the String.replace() and RegExp.exec() methods <br /><br /> var str:String = "Hi, Sev";<br /><br /> str = str.replace(/Hi, (w+)/,"$1, hello");<br /><br /> trace(str); // Sev, hello<br /><br /> |
| $$ | Puts a $ in the replacement text (not really a sub-string reference, but sorta matches up with this stuff.) |
| $& | References the fully matched string |
| $` | References the string preceding the matched pattern |
| $’ | References the string following the matched pattern |
| ?: | Non-capturing Group Sometimes you don’t want a group to be captured for back references, etc. This is can be done by adding ?: at the start of the group. eg: /(?:group)/ |
(?P<name>.*): | Named groups When used in RegExp.exec(), the group will be stored under a property with the ‘name’. Note: this is an AS3 only Regular Expression syntax. |
Links
Here are some links to some sites that help with regular expressions.