Creating An ActionScript RegExp Whitelist

Steve Yegge makes me feel stupid. There I’ve said it. I’ve come clean and can move on and admit there’s a whole lot I don’t know about development. Actually, I’m going to have to go one step further and admit I know very little about development which is depressing. This biz is a time sink; a void function; a black hole that sucks you in and returns nothing from its infinite murky depths. I know–clever. I’m probably trying a bit too hard to prove that my little ol’ social science degree gives me a leg up on someone, somewhere.

With that out of the way we can move on to another deficiency of mine–regular expressions. I don’t know diddly squat here, and usually avoid them like the plague or hope that Google or some fancy tool will just magically save my ass. There’s something about that compact, no white space syntax with all of its black magic quantifiers, metacharacters and metasequences and obtuse flags that brings out that first day of kindergarten feeling and sends me off to my feed reader. Oh, but back to the point. The other day I was asked to use a whitelist to validate filenames that the Screencast.com server can’t handle. It had been a year since I even wrote a regular expression, so of course I was crapping my pants. Obviously, I immediately Googled ActionScript RegExp whitelist, but came away with nada.

So there I was, left forlornly looking at the Flash / Flex docs with the realization that I was going to have to figure this one out on my own. And so I dug in and spent an hour or two reading the docs and playing around in the Flash script window (its still my choice for just sitting around and riffing, even though I spend most of my time staring at butt-ugly Eclipse–I’ll take the ugly though since Eclipse makes me “feel like a real developer”). Turns out RegExp are both a nightmare and not so bad. I think this is like the 5th time I’ve said that to myself though, so maybe its just safer to conclude I’m an idiot. Regardless, I was able to take a list of valid characters that the server folks handed over and determine if any filename the user wants to upload includes invalid characters (yep, now you know why Yegge makes me feel stupid).

On the off chance there’s some other developer mortified off their ass and desperately Googling for an ActionScript RegExp whitelist I’ve included a screenshot of some basic stuff that might help you out.

whitelist_actionscript.png

Oh and since some people get angry when they try to copy and paste code out of screen grabs, here’s some text (I’m too lazy to mark it up to look pretty which is why the screenshot came first).

// some simple basics
// no constructor expression literal
var p1:RegExp = /[a-z]/i; // pattern is case insensitive search for any a through z character
// the i is a flag indicating case insensitivity

// same expression using a constructor
var p2:RegExp = new RegExp( “[a-z]“, “i” );

var p3:RegExp = /[^a-z]/i; // whitelist - any character other than a-z will be detected
// the ^ is a negation operator when used inside, left of char brackets
// negation is the key to a whitelist pattern match (as far as I can tell)

// the i flag indicates case insensitivity
// the g flag means the pattern should be treated globally - important ramifications when matching
// more than a single occurence.
var pattern:RegExp =/[^a-z0-9\-'!#\(\)&=\[\]_~\^\.,\s]/ig; //this is a whitelist of valid characters

var data:String = “Bad @fubar% |.jpg”; // a String I’d like to check against my whitelist

// true means a character that is not in our whitelist exists
var invalid:Boolean = pattern.test( data );

trace( “invalid: ” + invalid );

// use String’s match method to get an Array holding invalid characters (those not found in whitelist)
var matchChars:Array = data.match( pattern );

trace( matchChars.join( ” ” ) ); // print invalid characters found

trace( “\n\n ———————” );

// recursively find all matches and their index
// the global flag must be set on the pattern, or else we get infinite recursion

var match:Object = {};

while ( match != null )
{
match = pattern.exec( data );
if ( match != null )
{
trace ( match.index + “: ” + match[0] );
}
}

Grant’s free RegExr tool is also helpful when you’re trying to gain a better understanding of regular expressions, but I found I had to dig in a little and understand some basics (again) before I could come back to it (you guessed it, Grant makes me feel like an idiot as well).

I’m also a fan of RegexBuddy, but alack my Windows days are long behind me and they haven’t stayed current with cross-platform coolness.

2 Comments

  1. Posted July 3, 2008 at 11:01 am | Permalink

    Here’s a neat service for easily marking up ActionScript (and many other programming languages) into HTML: http://xzfv.appspot.com/s/format.html. Credit to Mike Chambers.

  2. Posted July 4, 2008 at 3:13 am | Permalink

    Generally speaking, I hate those “learn quantum mechanics in 10 mintues” books, but there’s a “Regular Expressions in 10 minutes” by Ben Forta which, at least for me, was very clear, to the point, and serves as a great reference.

One Trackback

  1. By Bookmarks about Actionscript on December 28, 2008 at 8:00 am

    [...] - bookmarked by 4 members originally found by HolidayMell on 2008-11-20 Creating An ActionScript RegExp Whitelist http://www.brooksandrus.com/blog/2008/07/02/creating-an-actionscript-regexp-whitelist/ - [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*