从JavaScript中剥离空格(%20)

时间:2014-02-11 23:36:45

标签: javascript

如何从以下代码中删除空格(%20)?

var newTab = {"url": config.searchEngines[newTabIndex].url.replace(new RegExp("%s", "g"), info.selectionText).replace(new RegExp("%S", "g"), info.selectionText)}

selectionText是在网页上突出显示的电话号码。显然,将20添加到数字会导致数字变得不正确。

3 个答案:

答案 0 :(得分:2)

查看decodeURIComponent()功能 http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp

var uri = config.searchEngines[newTabIndex].url,
    newTab = {"url": decodeURIComponent(uri)};

答案 1 :(得分:0)

只需删除空格而不是替换为%20,除非它们是字符串的一部分或var之后。

答案 2 :(得分:0)

改为使用

str=str.replace(/%20/g,"")

所以对于你的代码

var newTab = {"url": config.searchEngines[newTabIndex].url.replace("%20", ""), info.selectionText).replace(/%20/g,""), info.selectionText)}
相关问题