我有一个需要操作的 URL。我似乎无法用空格替换查询字符串中的所有“+”。
var url = window.location.replace(/+/g, ' ');
我在这里做错了什么?
或者有更好的方法吗?
最佳答案
replace()
is a method on window.location
, 但它不是 the one you think .你想在 location.href
上调用 replace()
.
var url = window.location.href.replace(/\+/g, ' ');
作为Vega answered ,请注意您还需要转义 +
因为它的 special meaning as a quantifier .
关于javascript - 如何在 JavaScript 中替换字符串中的多个 '+' 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10604065/