Regular expression to remove hostname and port from URL?

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

    It's very simple:

    ^\w+:.*?(:)\d*
    

    Trying to find second occurance of ":" followed by number and preceded by http or https.

    This works for below two cases

    Ex:

    http://localhost:8080/myapplication

    https://localhost:8080/myapplication

    Hope this helps.

提交回复
热议问题