删除URL中的最后一个目录

我正在尝试删除URL的最后一个目录部分。我的URL如下所示:

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

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

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

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

https://my_ip_address:port/undefined

我应该使用什么正则表达式来执行此操作

通过使用此代码,可以从Url链接中删除最后一个元素

url.substring(0,url.lastIndexOf('/');

发表评论