Totally new to programming so kindly excuse the silly question. I have this URL http://www.blahblah.com/some/thing/blah ..
Now i need the string that appears after the third"/" ie., just "/some/thing".. I dont want to go from reverse as the URL is different for each page but the protocol and the host name is the same.. Is there a way i can extract "/some/thing" along using Javascript.. Appreciate any help.. Thanks!!
var part = string.split('/')[2]
Or more complicated:
var string = "/a/b/c/d/e/d"
var arr1 = string.split('/')
var arr2 = []
for (i in arr1)
{
if (arr1[i].length > 0)
arr2.push(arr1[i])
}
var part = arr2[2]
using the string.split method: http://www.w3schools.com/jsref/jsref_split.asp
var str = "www.blah.com/blah/blah/thing/blah.php";
var arrayOfString = str.split("/");
alert(arrayOfString[2]); //coming back to do regex/substring for you
have fun! :)
By the way... if you are new to web development, www.w3schools.com was extremely good to me :)
so to respond to your edit you need to do something like this :
assuming above already:
alert(arrayOfString.slice(3).join("/"));
ok fixed it based on actual needs here.. but like i said, check out http://www.w3schools.com it really taught me some good basics.
to fix for the fail case in the comments do this before the split/slice/join:
if (str.substring(0,1) == "/"){ str = str.substring(1);}
I would use a simple regular expression here.
One way to get stuff after the third "/" just throw it away with everything before it .
Basic idea:
"/a/b/c/d/e/f".replace(/.*?\/.*?\/s.*?\//, "")
// -> "c/d/e/f"
Alternative #1, to reduce repeat:
"/a/b/c/d/e/f".replace(/(?:.*?\/){3}/, "")
Alternative #2, if input can contain newlines:
"/a/b/c/d/e/f".replace(/(?:[^\/]*\/){3}/, "")
The *?
qualifier means "don't be greedy" so .
won't skip over /
's. It's not needed in the negative character class case because [^\\/]
will never match /
.
This could of course be written in a "less generic" form because because the real intent is just to git rid of the protocol/domain portions of the URI (the number of /
's is only an observed relationship):
"http://foo/a/b/c/d/e/f".replace(/https?:\/\/[^\/]*\/?/i, "")
// -> "a/b/c/d/e/f"
Note that the last \\/
has a ?
after it so it will return "" for "http://foo" and the i
is so it will match HTTP://
if that is used for some strange reason (completely optional, of course).
"HTTPS://foo".replace(/https?:\/\/[^\/]*\/?/i, "")
// -> ""
Happy coding!
Note: all of the above regular expressions will extract the content after the 3rd /
, as stated in the question. They will need to be slightly modified to include the 3rd /
in the result. Also, it helps to not ask XY questions.
var str = 'http://www.blahblah.com/some/thing/blah'
var afterThirdSlash = str.split('/').slice(3).join('/')
The split
returns an array which is the string split on every /
character. The slice
selects the part of the array from the fourth element (since arrays are zero-based) onwards, since that corresponds to the part after the third slash. The join
pieces these elements back together by re-inserting the /
. Is this what you wanted?
如果您知道网址以“ http://www.blahblah.com/”开头,为什么不使用url.substring(24)?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.