Getting parts of a URL (Regex)

后端 未结 26 2129
说谎
说谎 2020-11-22 02:13

Given the URL (single line):
http://test.example.com/dir/subdir/file.html

How can I extract the following parts using regular expressions:

  1. The Subd
相关标签:
26条回答
  • 2020-11-22 03:12
    //USING REGEX
    /**
     * Parse URL to get information
     *
     * @param   url     the URL string to parse
     * @return  parsed  the URL parsed or null
     */
    var UrlParser = function (url) {
        "use strict";
    
        var regx = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/,
            matches = regx.exec(url),
            parser = null;
    
        if (null !== matches) {
            parser = {
                href              : matches[0],
                withoutHash       : matches[1],
                url               : matches[2],
                origin            : matches[3],
                protocol          : matches[4],
                protocolseparator : matches[5],
                credhost          : matches[6],
                cred              : matches[7],
                user              : matches[8],
                pass              : matches[9],
                host              : matches[10],
                hostname          : matches[11],
                port              : matches[12],
                pathname          : matches[13],
                segment1          : matches[14],
                segment2          : matches[15],
                search            : matches[16],
                hash              : matches[17]
            };
        }
    
        return parser;
    };
    
    var parsedURL=UrlParser(url);
    console.log(parsedURL);
    
    0 讨论(0)
  • 2020-11-22 03:12

    I build this one. Very permissive it's not to check url juste divide it.

    ^((http[s]?):\/\/)?([a-zA-Z0-9-.]*)?([\/]?[^?#\n]*)?([?]?[^?#\n]*)?([#]?[^?#\n]*)$

    • match 1 : full protocole with :// (http or https)
    • match 2 : protocole without ://
    • match 3 : host
    • match 4 : slug
    • match 5 : param
    • match 6 : anchor

    work

    http://
    https://
    www.demo.com
    /slug
    ?foo=bar
    #anchor
    
    https://demo.com
    https://demo.com/
    https://demo.com/slug
    https://demo.com/slug/foo
    https://demo.com/?foo=bar
    https://demo.com/?foo=bar#anchor
    https://demo.com/?foo=bar&bar=foo#anchor
    https://www.greate-demo.com/
    

    crash

    #anchor#
    ?toto?
    
    0 讨论(0)
提交回复
热议问题