user3299413
user3299413

Reputation: 1

Stripping spaces (%20) from JavaScript

How can I remove spaces (%20's) from the following code?

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. Obviously adding 20 to the number causes the number to become incorrect.

Upvotes: 0

Views: 94

Answers (3)

Tim Vermaelen
Tim Vermaelen

Reputation: 7069

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

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

Upvotes: 2

scrblnrd3
scrblnrd3

Reputation: 7426

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)}

Upvotes: 0

bits
bits

Reputation: 8340

Just remove spaces instead of replacing with %20, unless they are part of a string or after var.

Upvotes: 0

Related Questions