Regular Expressions in AS3
On January 9, 2008, Flash / Flex - 2 CommentsFor the past 2 hours I’ve been struggling with the regular expression engine of AS3. Normally, when I need to write an regexp, I test it in the Regular Expression Workbench, a .NET app which is quite handy. The problem however is that it uses the .NET syntax for regexes. Apparently, each programming language uses it’s own regular expression syntax. For example, in .NET you can create a named group using (?<groupname>), in AS3 you need to type (?P<groupname>).And here comes the trouble… The named I create are only visible when using the Regexp.exec(string) method. But I cannot use this method, I need the string.replace() method!
How the hell do I call a groupname in a replace string. One would say using $groupname, but it doesn’t seem to work.
In order to prevent these problems in the future, I’ve written a small flex application to test regexps, it’s still a fairly simple beta version, but it works for me (for now). I will expand this application in the future.
If someone knows how to use groups in a replace, please tell me how!
What others have to say:
Hi,
Saw your post, very handy app.
I’ve got some help for you too…here’s how to use groups on replace in AS3:
string.replace(/whatever:(?P(group selection))/gi, “whatever you like $1″);
note that this only works with numbered, not named references (don’t ask me why though!)
Thanks, I figured that out also, apparently they only implented named references in the exec() method, not in the replace method.
Leave a Reply