Get relative URL from absolute URL

后端 未结 8 1967
自闭症患者
自闭症患者 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

    Don't use low-level stuff like regexp etc. These things have been solved by so many other people. Especially the edge cases.

    Have a look at URI.js, it should do the job: http://medialize.github.io/URI.js/docs.html#relativeto

    var uri = new URI("/relative/path");
    // make path relative
    var relUri = uri.relativeTo("/relative/sub/foo/sub/file"); // returns a new URI instance
    // relUri == "../../../path"
    

提交回复
热议问题