alienavatar
alienavatar

Reputation: 299

escape & in url

How can I escape & (ampersand) in url with the correct replacement in the jQuery function. I have tried

.replace("/&/g", "&")  
.replace("/&/g", "%26") 
.replace("/&/g", "\&") 

But nothing is working.

If anyone has idea please share with me.

Thanks

Upvotes: 4

Views: 1680

Answers (3)

intafon
intafon

Reputation: 196

Stumbled on this old posting looking for something else. Just wanted to add that the original poster's question had to do with asking why

string.replace("/&/g", "&")

wasn't working. The appropriate syntax for this should have been:

string.replace(/&/g, "&") 

The search token, as it was intended to be a regex, needs to be a regex and not a string.

Upvotes: 0

Justin Ethier
Justin Ethier

Reputation: 134167

If you need to just escape a URL, I recommend using a JavaScript function such as encodeURIComponent.

Upvotes: 9

Chris Schmitz
Chris Schmitz

Reputation: 8247

Try the URLEncode plugin: http://plugins.jquery.com/project/URLEncode

Upvotes: 2

Related Questions