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

发布于 2024-12-07 02:10:45 字数 179 浏览 0 评论 0原文

假设我有一个 URL:

http://something.com/somethingheretoo

并且我想获取 / 第三个实例之后的内容?

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

Let's say I have a 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

拥抱我好吗 2024-12-14 02:10:45

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

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

这给出如果您想要的段中有多个斜杠,您会更加灵活。

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.

阿楠 2024-12-14 02:10:45
let s = 'http://something.com/somethingheretoo';
parts = s.split('/');
parts.splice(0, 2);
return parts.join('/');
let s = 'http://something.com/somethingheretoo';
parts = s.split('/');
parts.splice(0, 2);
return parts.join('/');
感性不性感 2024-12-14 02:10:45

尝试类似下面的函数,它将返回搜索字符串 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"

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

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.

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.

千纸鹤带着心事 2024-12-14 02:10:45

如果您想坚持使用 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);

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

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

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

If you want to stick to 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:

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 /.

夏了南城 2024-12-14 02:10:45

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

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

splittedWord[0] 将返回“me”

splittedWord[1] 将返回“you”

splittedWord[2] 将返回“something”

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

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

splittedWord[0] would return "me"

splittedWord[1] would return "you"

splittedWord[2] would return "something"

蓝天白云 2024-12-14 02:10:45

听起来您想要路径名。如果您使用的是浏览器,请随身携带 a 元素...

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

...并让它为您进行解析。

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

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

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

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

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/

泪之魂 2024-12-14 02:10:45

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

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

等数载,海棠开 2024-12-14 02:10:45

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

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

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

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?

My preference of the proposed solutions is

var url = "http://blablab/test/page.php";
alert(url.split("/")[3]);
//-> "test"
献世佛 2024-12-14 02:10:45

除了使用 indexOf 之外,还可以通过以下方式执行此操作:

const url = 'http://something.com/somethingheretoo';
const content = new URL(url).pathname.slice(1);

Inestead of using indexOf it is possible to do this this way:

const url = 'http://something.com/somethingheretoo';
const content = new URL(url).pathname.slice(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文