JavaScript indexOf() - how to get specific index

前端 未结 8 652
野趣味
野趣味 2021-01-18 02:02

Let\'s say I have a URL:

http://something.com/somethingheretoo

and I want to get what\'s after the 3rd instance of /?

8条回答
  •  既然无缘
    2021-01-18 02:35

    Here's a very cool way of handling this: How can I remove all characters up to and including the 3rd slash in a string?

    My preference of the proposed solutions is

    var url = "http://blablab/test/page.php";
    alert(url.split("/")[3]);
    //-> "test"
    

提交回复
热议问题