Lord Vermillion Lord Vermillion - 3 months ago 20
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("&", "&");
console.log(urlString);

var urlString = decodeURIComponent('@Model');
console.log(urlString);


What am i doing wrong??

Joe Joe
Answer

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, '&');
console.log(urlString);

Snippet example:

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