URLParser: a parser to get value of host, protocol, port, parameters from an URL using regular expression

Filetype-URL Problem Summary: Once I need to get the host, protocol, port, parameters from a given URL with Action-Script. I googled for a class named HTTPUtil developed by members of flexpasta.com, and got some tips about URLUtil of Flex SDK. However, HTTPUtil is used to resolve information from the URL of the page the SWF file embed in. URLUtil is useful, but a little complex. I need a simple method to resolve a given URL, and use the resolved information directly without extra processing.

Solution Summary: I decide to write a class named URLParser myself to satisfy my requirement using regular expression of action-script.

Search-256x256 Demo | DownloadDownload Full Project


	import mx.managers.ISystemManager;

	public class URLParser
		public static var url:String;

		public static var host:String = "";
		public static var port:String = "";
		public static var protocol:String = "";
		public static var path:String = "";
		public static var parameters:Object;	

		 public static function parse( url:String ) : void
			URLParser.url = url;
			var reg:RegExp = /(?P<protocol>[a-zA-Z]+) : \/\/  (?P<host>[^:\/]*) (:(?P<port>\d+))?  ((?P<path>[^?]*))? ((?P<parameters>.*))? /x;
			var results:Array = reg.exec(url);

			protocol = results.protocol
			host = results.host;
			port = results.port;
			path = results.path;
			var paramsStr:String = results.parameters;
				parameters = null;
				parameters = new Object();

				if(paramsStr.charAt(0) == "?")
					paramsStr = paramsStr.substring(1);
				var params:Array = paramsStr.split("&");
				for each(var paramStr:String in params)
					var param:Array = paramStr.split("=");
					parameters[param[0]] = param[1];

Description: I use regular expression to resolve what I want from the given URL. The explanation of the expression is below:

As the url is “http://localhost:8080/URLParserDemo.swf?debug=true”

(?P<protocol>[a-zA-Z]+) match the protocol


: \/\/ match “://”


(?P<host>[^:\/]*) match the host domain


(:(?P<port>\d+))? match the port, if any


((?P<path>[^?]*))? match the requested resource path of the host, if any


((?P<parameters>.*))? match the request parameters, if any


Following is the source of URLParserDemo.mxml:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://ntt.cc -->
			import mx.controls.Alert;
			import mx.utils.ObjectUtil;

			private var params:Array;

			private function initApp():void
				var classInfo:Object = ObjectUtil.getClassInfo(URLParser.parameters);
				if(classInfo.properties!=null && classInfo.properties.length>0)
					params = classInfo.properties;

	<mx:Panel width="100%" height="100%" title="HttpUtil Demo窶披€粘how the host info of the loaded swf">
		<mx:Form width="100%" height="100%">
			<mx:FormItem label="url">
				<mx:Label text="{ URLParser.url}"/>
			<mx:FormItem label="host">
				<mx:Label text="{ URLParser.host}"/>
			<mx:FormItem label="path">
				<mx:Label text="{ URLParser.path}"/>
			<mx:FormItem label="port">
				<mx:Label text="{ URLParser.port}"/>
			<mx:FormItem label="protocol">
				<mx:Label text="{ URLParser.protocol}"/>
			<mx:FormItem label="parameters" direction="horizontal">
				 <mx:Repeater dataProvider="{params}" id="paramList">
				 	<mx:Label text="{paramList.currentItem.localName}"/>
				 	<mx:Label text="{URLParser.parameters[paramList.currentItem.localName]}"/>

*You can find more HTTPUtil info on: http://www.flexpasta.com

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

5 Responses to “URLParser: a parser to get value of host, protocol, port, parameters from an URL using regular expression”

  1. Great class! I like it more than mx.utils.URLUtil which does the same type of thing but on a smaller scale.

    I am using URLUtil for the parameters though. If your URL is “something.html?id=3&debug=true” you can use
    var params:String = URLUtil.objectToString(URLParser.parameters, “&”, true);
    This will result in “id=3&debug=true”

  2. [...] http://ntt.cc/2008/08/07/urlparser-a-parser-to-get-value-of-host-protocol-port-parameters-from-an-ur... カテゴリー: as3, flex入門 タグ: as3, flex3, url解析 コメント (0) トラックバック (0) コメントをどうぞ トラックバックURL [...]

  3. isaac ewing says:

    this worked great except for one part… i couldn’t get the systemManager.loaderInfo.url section to work so i replaced it with ExternalInterface.call(“window.location.href.toString”) and it worked great, thank you for sharing!!!

  4. I’m not sure why but this website is loading incredibly slow for me. Is anyone else having this problem or is it a issue on my end? I’ll check back later on and
    see if the problem still exists.

Leave a Reply