Javascript – How to remove all characters up to and including the 3rd slash in a string

javascriptstring

I'm having trouble with removing all characters up to and including the 3 third slash in JavaScript. This is my string:

http://blablab/test

The result should be:

test

Does anybody know the correct solution?

Best Answer

To get the last item in a path, you can split the string on / and then pop():

var url = "http://blablab/test";
alert(url.split("/").pop());
//-> "test"

To specify an individual part of a path, split on / and use bracket notation to access the item:

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

Or, if you want everything after the third slash, split(), slice() and join():

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