Lord Vermillion Lord Vermillion - 1 year ago 97
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
Answer Source

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, '&') );

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download