Javascript 从 url 中删除最后一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10886727/
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-08-24 03:25:26  来源:igfitidea点击:

Remove last element from url

javascriptjquery

提问by Steven

I need to remove the last part of the url from a span..

我需要从跨度中删除 url 的最后一部分..

I have

我有

<span st_url="http://localhost:8888/careers/php-web-developer-2"
st_title="PHP Web Developer 2" class="st_facebook_large" displaytext="facebook"
st_processed="yes"></span></span>

And I need to take the st_urland remove the php-web-developer-2from it so it is just http://localhost:8888/careers/.

我需要st_url取出并php-web-developer-2从中删除,所以它只是http://localhost:8888/careers/.

But I am not sure how to do that. php-web-developer-2will not always be that but it won't have any /in it. It will always be a -separated string.

但我不知道该怎么做。php-web-developer-2不会总是那样,但它不会有任何/内容。它始终是一个-分隔的字符串。

Any Help!!??

任何帮助!??

采纳答案by thecodeparadox

$('span').attr('st_url', function(i, url) {
    var str = url.substr(url.lastIndexOf('/') + 1) + '$';
    return url.replace( new RegExp(str), '' );
});

DEMO

演示

回答by Gisway

as simple as this:

就这么简单:

var to = url.lastIndexOf('/');
to = to == -1 ? url.length : to + 1;
url = url.substring(0, to);

回答by Nishu Tayal

Use this.

用这个。

$('span').attr('st_url', function(i, url) {
    var to = url.lastIndexOf('/') +1;

    x =  url.substring(0,to);
    alert(x);
})?

You can see Demo

你可以看演示

回答by xtofl

You could use a regular expression to parse the 'last piece of the url':

您可以使用正则表达式来解析“最后一段 url”:

var url="http://localhost:8888/careers/php-web-developer";

var baseurl=url.replace(
    new RegExp("(.*/)[^/]+$"),
    "") );

The RegExp thing basically says: "match anything, then a slash and then all non-slashes till the end of the string".

RegExp 的内容基本上是说:“匹配任何内容,然后是斜线,然后是所有非斜线,直到字符串的末尾”。

The replace function takes that matching part, and replaces it with the "anything, then a slash" part of the string.

replace 函数获取匹配的部分,并将其替换为字符串的“任何东西,然后是斜线”部分。

RegexBuddyhas a great deal of information on all this.

RegexBuddy有大量关于这一切的信息。

回答by nachito

You can see it work here: http://jsfiddle.net/xKxLR/

你可以在这里看到它的工作:http: //jsfiddle.net/xKxLR/

var url = "http://localhost:8888/careers/php-web-developer-2";
var regex = new RegExp('/[^/]*$');
console.log(url.replace(regex, '/'));

回答by Derk

Here is a slightly simpler way:

这是一个稍微简单的方法:

url = url.slice(0, url.lastIndexOf('/'));

回答by Kamran Amini

First you need to parse the tag. Next try to extract st_url value which is your url. Then use a loop from the last character of the extracted url and omit them until you see a '/'. This is how you should extract what you want. Keep this in mind and try to write the code .

首先,您需要解析标签。接下来尝试提取 st_url 值,即您的 url。然后使用从提取的 url 的最后一个字符开始的循环并省略它们,直到看到“/”。这就是你应该如何提取你想要的。记住这一点并尝试编写代码。