split() hacks & RegExp

var path:String = "section/home.swf";

var deepLinkValue:String = path.split("/")[1].split(".swf")[0];

trace(deepLinkValue);
/*outputs:
home
*/

As a primarily self taught programmer there was a time when I didn’t know what a regular expression was. There was also a time when ActionScript didn’t have them (you had to use a library). Sometimes when I’m in a rush I still do weird things like the above instead of regular expressions… pretty nasty I know.

Here are two different regular expressions that do the same thing:

var path = "section/home.swf"

trace(path.match(/w+(?=.)/))
/*outputs:
home
*/
 
trace(path.replace(/.+/|..+/g, ""))
/* also outputs:
home
*/

Actually, I’d be curious to see other regular expressions that only return one value (not an array) that achieve this same thing… feel free to post of a comment if you can think of one…

Leave a Reply

Your email address will not be published. Required fields are marked *