简体   繁体   English

我如何从第三个“ /”之后获取字符串

[英]How do i get the string from after the third “/”

Totally new to programming so kindly excuse the silly question. 对编程完全陌生,所以请原谅这个愚蠢的问题。 I have this URL http://www.blahblah.com/some/thing/blah .. 我有这个网址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!! 现在,我需要出现在第三个“ /”之后的字符串,即只是“ / some / thing”。我不想从反向开始,因为每个页面的URL不同,但是协议和主机名相同..有没有办法我可以使用Javascript提取“ / some / thing”呢。感谢任何帮助..谢谢!

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 使用string.split方法: 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 :) 顺便说一下...如果您是Web开发的新手,www.w3schools.com对我来说非常好:)

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. 好的,可以根据实际需要在这里修复它。.但是就像我说的那样,请访问http://www.w3schools.com,它确实教会了我一些良好的基础知识。

to fix for the fail case in the comments do this before the split/slice/join: 要修复注释中的失败情况,请在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: 备选方案1,以减少重复:

"/a/b/c/d/e/f".replace(/(?:.*?\/){3}/, "")     

Alternative #2, if input can contain newlines: 备选方案2,如果输入内容可以包含换行符:

"/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): 当然,这可以用“不太通用”的形式来写,因为真正的目的只是为了摆脱URI的协议/域部分( /的数目只是观察到的关系):

"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). 在此之后,它将为“ http:// foo”返回“”,而i则将与HTTP://匹配(如果出于某种奇怪的原因使用了HTTP:// ,当然是完全可选的)。

"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. 注意:以上所有正则表达式都将在问题3nd / 之后提取内容。 They will need to be slightly modified to include the 3rd / in the result. 它们将需要稍作修改以将3rd / 包含在结果中。 Also, it helps to not ask XY questions. 同样,它有助于不问XY问题。

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. split返回一个数组,该数组是每个/字符上拆分的字符串。 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. slice从第四个元素开始选择数组的一部分(因为数组从零开始),因为它对应于第三个斜杠之后的部分。 The join pieces these elements back together by re-inserting the / . join通过重新插入件一起这些元素回/ Is this what you wanted? 这就是你想要的吗?

如果您知道网址以“ http://www.blahblah.com/”开头,为什么不使用url.substring(24)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM