Using Regular Expressions in AS3
If you’re programming experience is primarily based in the world of Flash, you may not have had much exposure to regular expressions. With it’s inclusion in AS3, we now have some very powerful, and helpful tools available to us. I just wanted to post a quick snippet of how you might use regular expressions in your development.
[as]//regex that finds and replaces tokens in a string
function replaceTokens(str:String,paramObj:Object) : String{
//token structure ${tokenName}
var myRegEx:RegExp = /\$\{([a-zA-Z0-9]+)\}/g;
return str.replace(myRegEx,function(){return paramObj[arguments[1]]});
}[/as]
The regular expression we are concerned with is:
[as]var myRegEx:RegExp = /\$\{([a-zA-Z0-9]+)\}/g;[/as]
When this pattern is run against some input it matches the dollar sign, then an open brace, any letter or number and then a closing brace. Like so: ${exampleToken} The function we put together takes those matches and replaces the value with a match in the paramObj.
[...] make sure you check out his post on RegEx and replacing tokens. I can’t tell you enough how powerful this is to a Flash [...]
Pingback by The Flash Art of War » Blog Archive » Hiddentrak Flash Blog — July 2, 2008 @ 8:23 am
I’ve always wanted to learn more on RegEXP. I took a break form it but this post just made me think about it again. Might read up more on it. Thanks for the reminder.
CG
Comment by Clemente — July 31, 2008 @ 1:39 pm