我有一个地址并想更改一些元素,我的地址在下面:var address = "http://20.0.1.8/x-manufacturer/senders/d7aa5a30-681d-4e72-92fb-f0ba0f6f4c3e/sample-data";我想将var地址“sample=data”的最后一个索引的内容更改为“new-data”,我写了下面的代码来解决它,但没有给我我需要的东西!let array_address = address.split('/');var new address = array_address.splice(0,9,"new-data");//It just remove last index,didn't replace.
1 回答

开心每一天1111
TA贡献1836条经验 获得超13个赞
我假设您不知道地址的最后一位是什么
let url = new URL("http://20.0.1.8/x-manufacturer/senders/d7aa5a30-681d-4e72-92fb-f0ba0f6f4c3e/sample-data")
let path = url.pathname.split("/")
path[path.length-1] = "new-data"
url.pathname=path.join("/")
console.log(url.href)
添加回答
举报
0/150
提交
取消