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!
http://www.robgonda.com/blog/trackback.cfm?B925AD80-3048-7431-E437088F37B5F731
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.
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
@Micah, are you still using AS2? Hehe, I thought this was way gone :)
^[\\(]{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?
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.