JavaScript indexOf() - 如何获取特定索引 [英] JavaScript indexOf() - how to get specific index
本文介绍了JavaScript indexOf() - 如何获取特定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个网址:
http://something.com/somethingheretoo
我希望得到 /
的第3个实例后的内容?
and I want to get what's after the 3rd instance of /
?
等同于 indexOf()
,它允许我输入我想要的反斜杠的哪个实例。
something like the equivalent of indexOf()
which lets me input which instance of the backslash I want.
推荐答案
如果您知道它以 http://
或 https://
,只需使用此单行跳过该部分:
If you know it starts with http://
or https://
, just skip past that part with this one-liner:
var content = aURL.substring(aURL.indexOf('/', 8));
如果你想要的那个段中有多个斜杠,这会给你更大的灵活性。
This gives you more flexibility if there are multiple slashes in that segment you want.
这篇关于JavaScript indexOf() - 如何获取特定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文