admin管理员组

文章数量:1193330

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?

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?

Share Improve this question edited Nov 28, 2011 at 11:00 Andy E 344k86 gold badges480 silver badges450 bronze badges asked May 11, 2011 at 14:22 FrankFrank 5092 gold badges10 silver badges22 bronze badges 1
  • 1 Do you always the need the text after the LAST slash or after the THIRD slash? – Aravindan R Commented May 11, 2011 at 14:24
Add a comment  | 

6 Answers 6

Reset to default 18

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"
var string = 'http://blablab/test'
string = string.replace(/[\s\S]*\//,'').replace(/[\s\S]*\//,'').replace(/[\s\S]*\//,'')
alert(string)

This is a regular expression. I will explain below

The regex is /[\s\S]*\//

/ is the start of the regex

Where [\s\S] means whitespace or non whitespace (anything), not to be confused with . which does not match line breaks (. is the same as [^\r\n]).

* means that we match anywhere from zero to unlimited number of [\s\S]

\/ Means match a slash character

The last / is the end of the regex

var str = "http://blablab/test";
var index = 0;
for(var i = 0; i < 3; i++){
    index = str.indexOf("/",index)+1;
}
str = str.substr(index);

To make it a one liner you could make the following:

str = str.substr(str.indexOf("/",str.indexOf("/",str.indexOf("/")+1)+1)+1);

You can use split to split the string in parts and use slice to return all parts after the third slice.

var str = "http://blablab/test",
    arr = str.split("/");
arr = arr.slice(3);
console.log(arr.join("/"));  // "test"

// A longer string:
var str = "http://blablab/test/test";  // "test/test";

You could use a regular expression like this one:

'http://blablab/test'.match(/^(?:[^/]*\/){3}(.*)$/);
// -> ['http://blablab/test', 'test]

A string’s match method gives you either an array (of the whole match, in this case the whole input, and of any capture groups (and we want the first capture group)), or null. So, for general use you need to pull out the 1th element of the array, or null if a match wasn’t found:

var input = 'http://blablab/test',
    re = /^(?:[^/]*\/){3}(.*)$/,
    match = input.match(re),
    result = match && match[1]; // With this input, result contains "test"

let str = "http://blablab/test";
let data = new URL(str).pathname.split("/").pop();
console.log(data);

本文标签: javascriptHow can I remove all characters up to and including the 3rd slash in a stringStack Overflow