Get specific part of url of a link

后端 未结 4 1101
耶瑟儿~
耶瑟儿~ 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 03:09

    I'd suggest:

    var link = 'http://www.example.com/directory1/directory2/directory3/directory4/index.html';
    
    console.log(link.split('/')[5]);​
    

    JS Fiddle demo.

    The reason we're using [5] not [4] is because of the two slashes at the beginning of the URL, and because JavaScript arrays are zero-based.

提交回复
热议问题