سؤال

I wrote a JavaScript replica of PHP's parse_url function.

As far as I have wrote by now, the fucnction does the job and returns a JS object...

But is there a better way to do this:

function parse_url( url ) {
    if(typeof url == 'string') {
        // output following: scheme, host, user, pass, path, query, fragment
        var output = {};

        var split_scheme = url.split('//');

        // now we assume that we have: sheme, and the rest of the url after //
        if(split_scheme.length == 2) {
            // now we have the "scheme"
            // do not add if this URL is provided: //hostname/path?query=value#anchor
            if(split_scheme[0].length) {
                output.scheme = split_scheme[0].replace(':', '');
            }

            // we're now splitting the URL on first slash /
            // and assume that we'll get: host, (user and pass, if any);

            var split_url = split_scheme[1].split('/');

            if(split_url.length == 2) {
                // check if user/pass are provided
                var split_auth_hostname = split_url[0].split('@');

                output.host = split_auth_hostname[1];

                if(split_auth_hostname.length == 2) {
                    // now split the auth part of the hostname with ":"
                    var split_user_info = split_auth_hostname[0].split(':');

                    if(split_user_info.length == 2) {
                        // assume that both user and pass are provided now
                        output.user = split_user_info[0];
                        output.pass = split_user_info[1];
                    } else {
                        // assume that only "user" is provided
                        output.user = split_user_info[0];
                    }
                } else {
                    // assume that no auth info was provided in the URL
                    // first splitted element is the actual hostname
                    output.host = split_auth_hostname[0];
                }

                // now let's split the query/anchor from path
                var split_query = split_url[1].split('?');

                output.path = '/' + split_query[0];

                if(split_query.length == 2) {
                    // now split the anchor out of query string
                    var split_anchor = split_query[1].split('#');

                    // add the query without anchor
                    output.query = split_anchor[0];

                    // add anchoer
                    if(split_anchor.length == 2) {
                        output.fragment = '#' + split_anchor[1];
                    }
                } else {
                    output.query = split_query[0];
                }
            }
        }

        return output;
    }
}

I've created a demo jsfiddle here.

لا يوجد حل صحيح

نصائح أخرى

Performance test from jsPerf indicates using regular expression will be faster.

I used the regular expression from javascript the good parts. This is a textbook example and it runs 73% faster than your code so expect production quality code to do better and equal in the worst case.

URL to benchmark http://jsperf.com/parse-url.

And the code is used is:

function parse_url_regex(url) {
  var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;

  var result = parse_url.exec(url);

  return result;

}

But I highly suggest you try codereview.stackexchange.com

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top