Get specific part of url of a link

后端 未结 4 1103
耶瑟儿~
耶瑟儿~ 2021-01-17 02:40

I want to get a specific part of a url between the third and fourth slashes of a link on the page.

EDIT: Sorry I don\'t think I was clear the first time, I meant get

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-17 02:56

    var getSegment = function (url, index) {
       return url.replace(/^https?:\/\//, '').split('/')[index];
    }
    

    Usage:

    getSegment("http://domain.com/a/b/c/d/e", 4); // "d" 
    

    The replace makes sure that the first two slashes after the protocol (http or https) don't count.

提交回复
热议问题