Thursday, March 11, 2010

Regular Expressions in Actionscript 2

Regular Expressions are a powerful method of working with text strings based on dynamic patterns. For example, you might want to extract all the numbers from a string like "Michael travels 550km in 6.2 hours using 40L of gasoline", or replace any occurrence of "INV" followed by an arbitrary number of alphanumerics followed by a space with "- reference removed -".

Regular Expressions are useful in parsing user input or creating grammars for strings.

Flash decided not to support a RegExp class in Actionscript 2, though it is implemented in Actionscript 3, Javascript, and JScript.

Pavils Jurjans
bemoaned this fact and created his own RegExp class for Flash 5, which was encapsulated by Joey Lott into a .as file for Actionscript 2. Pavils also created a very handy tester, which together with websina's page, helped me to get started with regular expressions.


The CLIPS programmers have started to use our version of the class, which has been documented with ZenDocs, and includes a trim, rtrim, ltrim and slightly modified replace method. A simple tester .fla is available to interact with the class.

I probably feel as excited learning this new skill as the fellow in this xkcd.com comic.

1 comment:

gregbown said...

Thanks! Had a security issue with content spoofing and no way to check flashvars for illegal characters or outside domain. Yay! The world is safe again.