javascript 子字符串上的 indexOf 和 lastIndexOf 问题 - 从字符串中获取 URL 的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14359326/
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
Issues with indexOf and lastIndexOf on substring - Get Part of URL from string
提问by streetlight
I'm having a few issues breaking apart a string in the way I want. I have a url like this:
我在按照我想要的方式拆分字符串时遇到了一些问题。我有一个这样的网址:
http://SomeAddress.whatever:portWhatever/someDirectory/TARGETME/page.html
I'm trying to get the TARGETME portion on the string, using substring and indexOf instead of regex. This is the function I'm working with now:
我正在尝试使用 substring 和 indexOf 而不是正则表达式来获取字符串上的 TARGETME 部分。这是我现在正在使用的功能:
function lastPartofURL() {
// Finding Url of Last Page, to hide Error Login Information
var url = window.location;
var filename = url.substring(url.lastIndexOf('/')+1);
alert(filename);
}
However, when I wrote this I was targeting the 'page.html' part, so that's what it returns, but I'm having trouble reconfiguring this to do what I want it to do now.
然而,当我写这篇文章时,我的目标是“page.html”部分,所以这就是它返回的内容,但是我在重新配置它以完成我现在想要它做的事情时遇到了麻烦。
If possible, I would like it to originate from the beginning of the string rather than the end, as there should always be a url and then one directory before what I'm trying to target, but I'm interested in both solutions.
如果可能,我希望它从字符串的开头而不是结尾开始,因为在我尝试定位的内容之前应该总是有一个 url 和一个目录,但我对这两种解决方案都感兴趣。
Here is a regex that does something similar, but it's not secure (according to JSLint), and therefore I wouldn't mind replacing it with something more functional.
这是一个执行类似操作的正则表达式,但它不安全(根据 JSLint),因此我不介意用更实用的东西替换它。
/^.*\/.*\/TARGETME\/page.html.*/
采纳答案by Shadow Wizard is Ear For You
As already answered by others, .split()
is good for your case however assuming you mean to return "the part before last" of the URL (e.g. return "TARGETME" for http://SomeAddress.whatever:portWhatever/dirA/DirB/TARGETME/page.html
as well) then you can't use fixed number but rather take the item before last of the array:
正如其他人已经回答的那样,.split()
对您的情况有好处,但是假设您的意思是返回 URL 的“前一个部分”(例如,也返回“TARGETME” http://SomeAddress.whatever:portWhatever/dirA/DirB/TARGETME/page.html
),那么您不能使用固定数字,而是在此之前获取该项目数组的最后一个:
function BeforeLastPartofURL() {
var url = window.location.href;
var parts = url.split("/");
var beforeLast = parts[parts.length - 2]; //keep in mind that since array starts with 0, last part is [length - 1]
alert(beforeLast);
return beforeLast;
}
回答by Reinstate Monica Cellio
You can do it easier with string.split()
...
你可以更轻松地string.split()
...
function getPath() {
var url = window.location;
var path = url.split("/")[4];
alert(path);
return path;
}
I only suggest this method since you say you will always know the format of the URL.
我只建议使用这种方法,因为您说您将始终知道 URL 的格式。
回答by Anujith
回答by iJade
Try this
试试这个
function lastPartofURL() {
// Finding Url of Last Page, to hide Error Login Information
var url = window.location;
var sIndex = url.lastIndexOf('/');
var dIndex = url.lastIndexOf('.', sIndex);
if (dotIndex == -1)
{
filename = url.substring(sIndex + 1);
}
else
{
filename = url.substring(sIndex + 1, dIndex);
}
alert(filename);
}