post header image for dissectURL(url:String)

dissectURL(url:String)

16th January 2009

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("https://www.mikecann.blog/?p=294").protocol;

trace(protocol); It would trace "http".

Enjoy!

SUBSCRIBE TO FUTURE POSTS

COMMENT