我正在尝试删除 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('/'));
关于javascript - 删除 URL 中的最后一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16750524/