dissectURL(url:String)

Been a while since i have shared any coding tips, but here is one for those Actionscript 3 Coders out there. Its a helper function that splits a URL up into its different parts using RegExp.

public static function dissectURL(url:String) : Object
{
var keys : Array = [“source”, “protocol”, “authority”, “userInfo”, “user”, “password”, “host”, “port”, “relative”, “path”, “directory”, “file”, “query”, “anchor”];
var hostExtractPattern:RegExp = /^(?:([^:/?#]+):)?(?://((?:(([^:@]*):?([^:@]*))?@)?([^:/?#]*)(?::(d*))?))?((((?:[^?#/]*/)*)([^?#]*))(?:?([^#]*))?(?:#(.*))?)/;
var o : Object = hostExtractPattern.exec(url);
var r : Object = new Object();
for (var i:int = 0; i < keys.length; i++) { r[keys[i]] = o[i];    }
return r;
}

An example of its use would be:

var protocol : String = dissectURL(“http://www.mikecann.co.uk/?p=294”).protocol;
trace(protocol);

It would trace “http”.

Enjoy!