How to parse a URL?

前端 未结 6 616

If there is one thing I just cant get my head around, it\'s regex.

So after a lot of searching I finally found this one that suits my needs:

function         


        
6条回答
  •  忘掉有多难
    2020-11-27 03:39

    Please note that this solution is not the best. I made this just to match the requirements of the OP. I personally would suggest looking into the other answers.

    THe following regexp will give you back the domain and the rest. :\/\/(.[^\/]+)(.*):

    1. www.google.com
    2. /goosomething

    I suggest you studying the RegExp documentation here: http://www.regular-expressions.info/reference.html

    Using your function:

    function get_domain_name()
        { 
        aaaa="http://www.somesite.se/blah/sdgsdgsdgs";
        //aaaa="http://somesite.se/blah/sese";
            var matches = aaaa.match(/:\/\/(?:www\.)?(.[^/]+)(.*)/);
            alert(matches[1]);
            alert(matches[2]);
        }
    

提交回复
热议问题