Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

URL Parser

  •  11-05-2008, 12:29 PM

    URL Parser

    So, using JavaScript I have a parser for a URL:

       function parseUrl(url) {
          var e = /((http|https|ftp):\/\/)?((.*?)\/)?((.*)\/)?(.*)?/;
          if (url.match(e)) {

             return  { url:     RegExp['$&']
                , protocol: RegExp.$2
                , host:     RegExp.$4
                , path:     RegExp.$6
                , file:     RegExp.$7
                , hash:     RegExp.$8 };
          }
          else {
             return  {url:"", protocol:"",host:"",path:"",file:"",hash:""};
          }
       }

     
    As you can see, the RegExp is:  /((http|https|ftp):\/\/)?((.*?)\/)?((.*)\/)?(.*)?/

    Given a URL like: http://regexadvice.com/forums/AddPost.aspx?ForumID=68
    The variables would result:
    url:    http://regexadvice.com/forums/AddPost.aspx?ForumID=68
    protocol: http
    host:    regexadvice.com
    path:    forums
    file:     AddPost.aspx?ForumID=68
    hash:    

     -or-

    Given a URL like: AddPost.aspx?ForumID=68
    The variables would result:
    url:    AddPost.aspx?ForumID=68
    protocol:
    host:   
    path:   
    file:     AddPost.aspx?ForumID=68
    hash:   

     

     

    I haven't set up the hash portion, and I'm not worried about that yet.  So the above works exactly how I want it to.  The issue though is when something like below happens:


    Given a URL like: AddPost.aspx?ForumID=http://www.something.com/fldr/whatever.html
    The variables would result:
    url:    AddPost.aspx?ForumID=http://www.something.com/fldr/whatever.html
    protocol:
    host:    AddPost.aspx?ForumID=http:
    path:    /www.something.com/fldr
    file:     whatever.html
    hash:  

     

     How I would like it to work is like the above.  In this case, the desired result would be:

    Given a URL like: AddPost.aspx?ForumID=http://www.something.com/fldr/whatever.html
    The variables would result:
    url:    AddPost.aspx?ForumID=http://www.something.com/fldr/whatever.html
    protocol:
    host:   
    path:   
    file:     AddPost.aspx?ForumID=http://www.something.com/fldr/whatever.html
    hash:  

     

     

    Hope that makes sense.  I appreciate the help,

    vol7ron 

View Complete Thread