删除URL中的最后一个目录 [英] remove last directory in URL

查看:69
本文介绍了删除URL中的最后一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除URL的最后目录部分.我的网址看起来像这样:

I am trying to remove the last directory part of an URL. My URL looks like this:

https://my_ip_address:port/site.php?path=/path/to/my/folder.

单击按钮时,我想将其更改为

When clicking on a button, I want to change this to

https://my_ip_address:port/site.php?path=/path/to/my. (删除最后一部分).

https://my_ip_address:port/site.php?path=/path/to/my. (Remove the last part).

我已经尝试过window.location.replace(/\/[A-Za-z0-9%]+$/, ""),结果是

https://my_ip_address:port/undefined.

我应该使用什么正则表达式来做到这一点?

What Regex should I use to do this?

推荐答案

解释:用"/"展开,用pop删除最后一个元素,再用"/"联接.

Explanation: Explode by "/", remove the last element with pop, join again with "/".

function RemoveLastDirectoryPartOf(the_url)
{
    var the_arr = the_url.split('/');
    the_arr.pop();
    return( the_arr.join('/') );
}

请参见小提琴 http://jsfiddle.net/GWr7U/

这篇关于删除URL中的最后一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆