Get relative URL from absolute URL

后端 未结 8 1902
自闭症患者
自闭症患者 2020-12-03 04:42

I want to get the relative URL from an absolute URL in JavaScript using regex and the replace method.

I tried the following but it is not working:

va         


        
8条回答
  •  伪装坚强ぢ
    2020-12-03 05:10

    If by "relative URL" you mean the part of the string after the first single /, then it's simple:

    document.write(str.replace(/^(?:\/\/|[^/]+)*\//, ''));
    

    This matches all the characters up to the first single / in the string and replaces them with the empty string.

    In: http://localhost/my/page.jsp --> Out: /my/page.jsp

提交回复
热议问题