Lord Vermillion Lord Vermillion - 10 months ago 57
jQuery Question

Remove amp; from string in javascript

I pass a premade URL string as a model to my view in a MVC-project.

I pass the URL as an encodedURIComponent to my controller:

window.location = '@Url.Content("~/Default/UserAgreement?registerData=")' + encodeURIComponent(jsonHttp);

Then i pass the string as a model

public ActionResult UserAgreement(String registerData)
return View(null, null, System.Uri.UnescapeDataString(registerData));

Then when i try to log it i can't get rid of the
None of these work:

var urlString = '@Model'.replace("&", "&");

var urlString = decodeURIComponent('@Model');

What am i doing wrong??

Joe Joe

You probably just need to make your replace global with the /g modifier on a regex, otherwise it will only replace the first instance of &:

var urlString = '@Model'.replace(/&/g, '&');

Snippet example:

var str = 'Something & Else && Here';
alert( str );
alert( str.replace(/&/g, '&') );