Regular expression to remove hostname and port from URL?

后端 未结 6 2189
别跟我提以往
别跟我提以往 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:55

    Quick 'n' dirty:

    ^[^#]*?://.*?(/.*)$

    Everything after the hostname and port (including the initial /) is captured in the first group.

提交回复
热议问题