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
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
Share Improve this question edited Sep 3, 2011 at 22:12 Justin Ethier 134k52 gold badges232 silver badges287 bronze badges asked Jun 21, 2010 at 18:02 alienavataralienavatar 2991 gold badge7 silver badges19 bronze badges 2-
.replace(/&/g, "%26")
should work if your JS is external. If it's embedded in your HTML, then you probably need to escape the&
as&
. But, useencodeURIComponent
instead of reinventing the wheel. – Ateş Göral Commented Jun 21, 2010 at 18:07 - 1 This really has nothing to do with jQuery - string replacement operations are plain old JavaScript – gnarf Commented Jun 21, 2010 at 18:21
3 Answers
Reset to default 9If you need to just escape a URL, I remend using a JavaScript function such as encodeURIComponent.
Try the URLEncode plugin: http://plugins.jquery./project/URLEncode
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.