JavaScript indexOf() - 如何获取特定索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7570276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 00:35:04  来源:igfitidea点击:

JavaScript indexOf() - how to get specific index

javascriptindexof

提问by Itai Sagi

Let's say I have a URL:

假设我有一个 URL:

http://something.com/somethingheretoo

and I want to get what's after the 3rd instance of /?

我想知道第三个实例之后是/什么?

something like the equivalent of indexOf()which lets me input which instance of the backslash I want.

类似于indexOf()which 让我输入我想要的反斜杠实例的东西。

采纳答案by Kelly

If you know it starts with http://or https://, just skip past that part with this one-liner:

如果您知道它以http://或开头https://,只需使用此单行跳过该部分:

var content = aURL.substring(aURL.indexOf('/', 8));

This gives you more flexibility if there are multiple slashes in that segment you want.

如果您想要的该段中有多个斜杠,这将为您提供更大的灵活性。

回答by PiTheNumber

s = 'http://something.com/somethingheretoo';
parts = s.split('/');
parts = parts.slice(0, 2);
return parts.join('/');

回答by bjornruysen

Another approach is to use the Javascript "split" function:

另一种方法是使用 Javascript "split" 函数:

var strWord = "me/you/something";
var splittedWord = strWord.split("/");

splittedWord[0] would return "me"

splittedWord[0] 将返回“我”

splittedWord[1] would return "you"

splittedWord[1] 将返回“你”

splittedWord[2] would return "something"

splittedWord[2] 会返回“东西”

回答by user113716

It sounds like you want the pathname. If you're in a browser, keep an aelement handy...

听起来你想要pathname. 如果您在浏览器中,请将a元素放在手边...

var _a = document.createElement('a');

...and let it do the parsing for you.

...让它为你做解析。

_a.href = "http://something.com/somethingheretoo";

alert( _a.pathname.slice(1) );  // somethingheretoo

DEMO:http://jsfiddle.net/2qT9c/

演示:http : //jsfiddle.net/2qT9c/

回答by nnnnnn

Try something like the following function, which will return the index of the nth occurrence of the search string s, or -1 if there are n-1 or fewer matches.

尝试类似下面的函数,它将返回搜索字符串 s 第 n 次出现的索引,如果有 n-1 个或更少的匹配项,则返回 -1。

String.prototype.nthIndexOf = function(s, n) {
  var i = -1;
  while(n-- > 0 && -1 != (i = this.indexOf(s, i+1)));
  return i;
}

var str = "some string to test";

alert(str.nthIndexOf("t", 3)); // 15
alert(str.nthIndexOf("t", 7)); // -1
alert(str.nthIndexOf("z", 4)); // -1

var sub = str.substr(str.nthIndexOf("t",3)); // "test"

Of course if you don't want to add the function to String.prototype you can have it as a stand-alone function by adding another parameter to pass in the string you want to search in.

当然,如果您不想将函数添加到 String.prototype 中,您可以通过添加另一个参数来传递您要搜索的字符串,从而将其作为独立函数使用。

回答by Rob W

If you want to stick to indexOf:

如果你想坚持indexOf

var string = "http://something/sth1/sth2/sth3/"
var lastIndex = string.indexOf("/", lastIndex);
lastIndex = string.indexOf("/", lastIndex);
lastIndex = string.indexOf("/", lastIndex);
string = string.substr(lastIndex);

If you want to get the path of that given URL, you can also use a RE:

如果你想获取给定 URL 的路径,你也可以使用 RE:

string = string.match(/\/\/[^\/]+\/(.+)?/)[1];

This RE searches for "//", accepts anything between "//" and the next "/", and returns an object. This object has several properties. propery [1]contains the substring after the third /.

此 RE 搜索“ //”,接受“ //”和下一个“ /”之间的任何内容,并返回一个对象。这个对象有几个属性。属性[1]包含第三个 之后的子字符串/

回答by Pat

In your case, you could use the lastIndexOf()method to get the 3rd forward slash.

在您的情况下,您可以使用该lastIndexOf()方法获取第三个正斜杠。

回答by erlingormar

Here's a very cool way of handling this: How can I remove all characters up to and including the 3rd slash in a string?

这是一种非常酷的处理方法: 如何删除字符串中直到并包括第 3 个斜杠的所有字符?

My preference of the proposed solutions is

我对提议的解决方案的偏好是

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