Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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??

like image 292
Lord Vermillion Avatar asked May 23 '26 09:05

Lord Vermillion


1 Answers

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, '&') );
like image 181
Joe Avatar answered May 25 '26 12:05

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!