sevenson.com.au logo

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.

To top it all off, I 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.

You can check it all out right here…

Open Regular Expression Tester

Hope that helps you out!

Regular Expression Methods

Here is 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

Click here for the official documentation


RegExp.exec(string:String):Object

Returns an object with with various properties relating to the pattern. The properties are

  • [0],[1] etc – Array like access – [pattern, group1, group2]
  • 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

Click here 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, $`, $&, $’

Click here 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

Click here 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 [fullpattern, group 1, group2]
  • if the ‘g’ flag is set, the array will contain all the possible full pattern matches – [fullpattern1, fullpattern2]

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

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

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.

^ (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 – [A-Za-z0-9]
  • 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 [12345]

Meta Sequences

Meta Sequences are special string that can be used to represent specific characters in a pattern.

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

/<p>.*/</p>/ greedy – the * will match as many characters as possible
/<p>.*?/</p>/ 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.

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


6 thoughts on “Testing Regular Expressions in ActionScript 3

  1. Great post and app too. it’s really handy while trying out new expression. i use it a lot! thanks!
    *ctrl+d*

  2. Cool tool, but it doesn’t seem to work for unicode characters.
    /\u00E9/g is not finding any occurrence of ‘é’ in my text.


Leave a Reply

Your email address will not be published. Required fields are marked *

Name *

This site uses Akismet to reduce spam. Learn how your comment data is processed.


sevenson.com.au