Regular expression to remove hostname and port from URL?

后端 未结 6 2186
别跟我提以往
别跟我提以往 2020-12-15 08:55

I need to write some javascript to strip the hostname:port part from a url, meaning I want to extract the path part only.

i.e. I want to write a function getPath(url

6条回答
  •  借酒劲吻你
    2020-12-15 09:43

    The window.location object has pathname, search and hash properties which contain what you require.

    for this page

    location.pathname = '/questions/441755/regular-expression-to-remove-hostname-and-port-from-url'  
    location.search = '' //because there is no query string
    location.hash = ''
    

    so you could use

    var fullpath = location.pathname+location.search+location.hash
    

提交回复
热议问题