parse URL with JavaScript or jQuery

后端 未结 5 1429
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 02:44

Ok lets say I have a URL

example.com/hello/world/20111020 (with or without the trailing slash). What I would like to do is strip from the url the domain example.com.

相关标签:
5条回答
  • 2020-12-17 03:24

    This should work

    var url = 'example.com/hello/world/20111020/';
    //get rid of the trailing / before doing a simple split on /
    var url_parts = url.replace(/\/\s*$/,'').split('/'); 
    //since we do not need example.com
    url_parts.shift(); 
    

    Now url_parts will point to the array ["hello", "world", "20111020"].

    0 讨论(0)
  • 2020-12-17 03:29
       <script type="text/javascript">
        function splitThePath(incomingUrl){
         var url = document.createElement("a");
         url.href = incomingUrl;
        //url.hash  Returns the anchor portion of a URL
        //url.host  Returns the hostname and port of a URL
        //url.hostname  Returns the hostname of a URL
        //url.href  Returns the entire URL
        //url.pathname  Returns the path name of a URL
        //url.port  Returns the port number the server uses for a URL
        //url.protocol  Returns the protocol of a URL
        //url.search    Returns the query portion of a URL
        if(url.pathname && url.pathname != ""){
       var pathnameArray = url.pathname.split("/");
     }else{
    
    }
    
    
    }
    </script>
    
    0 讨论(0)
  • 2020-12-17 03:29

    I have created the following regular expression for URLs

    ^https?://(((0|([1-9][0-9]{0,1}))(\.(0|([1-9][0-9]{0,1}))){3})|([a-zA-Z]([a-zA-Z0-9$\-_@\.&+!*"\'\(\),]|(%[0-9a-fA-F][0-9a-fA-F]))*(\.([a-zA-Z]([a-zA-Z0-9$\-_@\.&+!*"\'\(\),]|(%[0-9a-fA-F][0-9a-fA-F]))*))*))(/|((/([a-zA-Z]([a-zA-Z0-9$\-_@\.&+!*"\'\(\),]|(%[0-9a-fA-F][0-9a-fA-F]))*))*))$
    

    It has been written for MySql - I am sure with a bit of fiddling you can get it you work for your needs.

    BTW - I took the idea from an RFC - The number escapes me at this moment

    0 讨论(0)
  • 2020-12-17 03:44

    You can use the jQuery-URL-Parser plugin:

    var file = $.url.attr("file"); 
    

    In your case you'd probably want to use segment():

    var segments = $.url('http://allmarkedup.com/folder/dir/example/index.html').segment(); 
    
    // segments = ['folder','dir','example','index.html']
    
    0 讨论(0)
  • 2020-12-17 03:45

    For parsing URLs, one different approach can be using anchor DOM object.

    var a = document.createElement("A");
    a.href = 'http://example.com:8080/path/to/resources?param1=val1&params2=val2#named-anchor';
    
    a.protocol; // http:
    a.host; // example.com:8080
    a.hostname; //example.com
    a.port; // 8080 (in case of port 80 empty string returns)
    a.pathname; // /path/to/resources
    a.hash; // #named-anchor
    a.search // ?param1=val1&params2=val2
    
    0 讨论(0)
提交回复
热议问题