简体   繁体   English

从JavaScript中剥离空格(%20)

[英]Stripping spaces (%20) from JavaScript

How can I remove spaces ( %20 's) from the following code? 如何从以下代码中删除空格( %20 )?

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

selectionText is a phone number that is highlighted on a web page. selectionText是在网页上突出显示的电话号码。 Obviously adding 20 to the number causes the number to become incorrect. 显然,在数字上加20会导致数字变得不正确。

Check out the decodeURIComponent() Function http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp 查看decodeURIComponent()函数http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp

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

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

Use this instead 请改用它

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

so for your code 所以对你的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM