Rob Gonda's Blog

actionscript regular expression class

I still can't believe that Flash doesn't have a built-in regEx match or replace engine, but it can still be accomplished by using external classes. After vast research, I found a class that I really liked and I wrote a VERY simple example validating an email string.

This library is extremely useful because now you can apply patterns to validate numbers, dates, emails, urls, phones, etc. Most actionscripters are not hip or even aware that this is possible, and have to write dozens of lines to validate a simple email address.

You can download the class and the Flash test files, enclosed with this post.

Enjoy!

TrackBacks
There are no trackbacks for this entry.

Trackback URL for this entry:
http://www.robgonda.com/blog/trackback.cfm?B925AD80-3048-7431-E437088F37B5F731

Comments
If you didn't know, AS 3.0 has native Regular Expressions support.

More details here: http://labs.macromedia.com/wiki/index.php/ActionSc...
# Posted By arul Prasad | 3/2/06 12:29 AM
Arul, thanks; I actually did know... but I can't believe they didn't have it for the past 8 versions of Flash :)
There's some really nice things coming with 3.0 ... as if Flash 8 wasn't enough.
# Posted By Rob Gonda | 3/2/06 12:34 AM
I agree, we really are spoiled. I heard flash 10 is going to ship with free ice cream. :P
# Posted By Phil Douglas | 3/2/06 5:22 PM
Hi, I'm writing a research paper covering many of the problems that existed in Actionscript 2.0 and one of the sections covers the lack of regular expressions. I found your weblog while searching through several blogs discussing Actionscript's previous issues with regex and felt that the way you described the problem seemed to be most eloquent. I was wondering if you would mind if I quoted you in my paper as saying, "Most actionscripters are not hip or even aware that this is possible, and have to write dozens of lines to validate a simple email address."
# Posted By Aimee Ault | 11/30/06 6:24 PM
Aimee, not a problem. Ping me when your paper's done; I'd like to read it and link to it.

Cheers.
# Posted By Rob Gonda | 12/4/06 9:03 PM
The file cant be accessed, would you please help ?
# Posted By suchnight | 12/26/06 8:00 AM
The file has been restored.
# Posted By Rob Gonda | 12/27/06 6:02 PM
Hello! I couldn't find the file on your post, but I found one somewhere else, I guess it's the one you had used because there are not many many RegExp AS classes running around these days:

<a href="http://www.jurjans.lv/flash/RegExp.html">;
http://www.jurjans.lv/flash/RegExp.html</a>;

My life has changed when I discovered Regular Expressions. I'm an used PHP coder now, and life seemed to me so arid when moving on to Action Script, which, totally opposite of PHP, is very poorly populated with essential functions and handy shortcuts that make coding a snap.

By the way, I'm starting an AS function library named ASPHALT, containing AS versions of the most handy PHP functions, for new AS coders used to PHP, just like me. Anybody interested in contributing in any way should drop me a line: BigueNique@yahoo.ca
# Posted By Bigue Nique | 4/2/07 7:44 PM
You can find the version I used by clicking download, right underneath my post. The library I used is based on the same one you found, but somewhat modified. Thanks for the link nonetheless; it's good to have it.
Indeed, RegExs are extremely handy... post a link to your project when it becomes available...

Cheers.
# Posted By Rob Gonda | 4/3/07 10:36 PM
This function alternates between working and being broken on consecutive calls. For example:
var email_re = new RegExp('^([\\w\\-\\.]+)@(([\\w\\-]{2,}\\.)+[\\w\\-]{2,3})$', 'gim'),

trace(email_re.test('asdf@asdf.com'));
trace(email_re.test('asdf@asdf.com'));
trace(email_re.test('asdf@asdf.com'));
trace(email_re.test('asdf@asdf.com'));
trace(email_re.test('asdf@asdf.com'));
trace(email_re.test('asdf@asdf.com'));

returns:
true
false
true
false
true
false

Can someone confirm that I'm not going crazy.
# Posted By Micah | 8/1/08 9:51 AM
@Micah (and also @Rob Gonda)


I have a feeling that the flags property has something to do with this.

I used a setInterval() call to simulate what Micah did:


import RegExp;

var emailPattern_str:String = '^([\\w\\-\\.]+)@(([\\w\\-]{2,}\\.)+[\\w\\-]{2,3})$';
var flags:String = 'gim'; // global, case Insensitive, multiline
var email_txt:String = 'rob@ichameleongroup.com';

var email_re = new RegExp(emailPattern_str, flags);
setInterval(traceit, 500, email_re);

function traceit(ere)
{
   trace(ere.test(email_txt));
}


this traces out:
true
false
true
false
true
false
...

However if I set the flags-property to an empy string "" instead of "gim",

i get
true
true
true
true
true
true
true
true
true
...


My guess is that Rob never intended the check to run twice, hence not making this a bug. My car makes a hell of a lot of noise when I turn the ignition key when the engine is already running. It's just a matter of how things are intended to be used. It works the first time, that's the most important part.

Kudos to Rob for writing this beautiful class for those among us who are sometimes forced to still use AS2.

Best regards,
Marvelade
# Posted By Marvelade actionscript & PHP lab | 8/16/08 5:57 AM
@Marvelade, thanks for clarifying it for the group.

@Micah, are you still using AS2? Hehe, I thought this was way gone :)
# Posted By Rob Gonda | 8/18/08 11:28 PM
Yes I'm using actionscript 2. Also a lot random expressions I try using don't work at all for example this one for a phone number I got from regexlib.com:
^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$
Any ideas?
# Posted By Micah | 8/28/08 2:21 PM
Rob,

It seems that if I use rob.gonda@ichameleongroup.com it traces false. I'm just getting started with regular expressions so I can't correct the code yet. Is there a way to allow for the extra "." in the email address?

Thanks.
# Posted By rgreen | 9/3/08 7:55 AM
This blog is running version 5.9.003. Contact Blog Owner