Monday 27 July 2009

URL parsing in Javascript

There is very simple way to parse URL in Javascript. Just define new method in the prototype of the String. Of course, it has own shortcomings but in the most cases it covers the wide range of URLs and protocols (http(s), ftp, mailto, etc; the mandatory part of URL, host is considered as domain names, IPs, and localhost separately), and moreover it considers the more complex URLs like jdbc:oracle://localhost:1521.
if ( ! String.prototype.parseUrl ) {

/**
 * Considers the string object as URL and returns it's parts separately
 *
 * @param void
 * @return Object
 * @access public
 */
String.prototype.parseUrl = function()
{
    var matches = this.match(arguments.callee.re);

    if ( ! matches ) {
        return null;
    }

    var result = {
        'scheme': matches[1] || '',
        'subscheme': matches[2] || '',
        'user': matches[3] || '',
        'pass': matches[4] || '',
        'host': matches[5],
        'port': matches[6] || '',
        'path': matches[7] || '',
        'query': matches[8] || '',
        'fragment': matches[9] || ''};

    return result;
};

String.prototype.parseUrl.re = /^(?:([a-z]+):(?:([a-z]*):)?\/\/)?(?:([^:@]*)(?::([^:@]*))?@)?((?:[a-z0-9_-]+\.)+[a-z]{2,}|localhost|(?:(?:[01]?\d\d?|2[0-4]\d|25[0-5])\.){3}(?:(?:[01]?\d\d?|2[0-4]\d|25[0-5])))(?::(\d+))?(?:([^:\?\#]+))?(?:\?([^\#]+))?(?:\#([^\s]+))?$/i;

}

3 comments:

  1. Hello, Ildar.

    Could you, please, specify a license for this code? For example, BSD/MIT (best ;) ) or CDDL or GPL/LGPL...

    ReplyDelete
  2. Hi Vadim,

    Feel free using this code in your projects. Just do not forget to paste the link to the original source.

    ReplyDelete