JavaScript indexOf() - how to get specific index

前端 未结 8 583
野趣味
野趣味 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:39
    s = 'http://something.com/somethingheretoo';
    parts = s.split('/');
    parts = parts.slice(0, 2);
    return parts.join('/');
    
    0 讨论(0)
  • 2021-01-18 02:42

    In your case, you could use the lastIndexOf() method to get the 3rd forward slash.

    0 讨论(0)
提交回复
热议问题