Javascript 哈希替换错误

Ada*_*dam 0 javascript hash replace

这有什么问题吗?

哈希是:#/search=hello/somethingelse/

window.location.hash.replace(/search=([^\/]*)/gi, "search=" + value);
Run Code Online (Sandbox Code Playgroud)

编辑:

我只想改变一部分而hash不是全部hash

Ple*_*and 5

hash.replace()实际上并不改变哈希值,只返回一个值(因为它是一个字符串函数)。尝试使用以下方法分配该结果:

window.location.hash = window.location.hash.replace(/search=([^\/]*)/gi, "search=" + value);
Run Code Online (Sandbox Code Playgroud)

另一方面,它实际上window.location.replace() 一个更改 URL 的函数,但不能直接与正则表达式一起使用。