删除放大器; 来自javascript中的字符串

Lor*_*ion 1 asp.net-mvc jquery razor

我将一个预制的URL字符串作为模型传递给我在MVC项目中的视图.

我将URL作为encodedURIComponent传递给我的控制器:

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

然后我将字符串作为模型传递

   public ActionResult UserAgreement(String registerData)
    {
        return View(null, null, System.Uri.UnescapeDataString(registerData));
    }
Run Code Online (Sandbox Code Playgroud)

然后,当我尝试记录它时,我无法摆脱&这些工作:

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

var urlString = decodeURIComponent('@Model');
console.log(urlString);
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么??

Joe*_*Joe 5

您可能只需要在正则表达式上global使用/g修饰符替换,否则它只会替换第一个实例&:

var urlString = '@Model'.replace(/&/g, '&');
console.log(urlString);
Run Code Online (Sandbox Code Playgroud)

片段示例:

var str = 'Something & Else && Here';
alert( str );
alert( str.replace(/&/g, '&') );
Run Code Online (Sandbox Code Playgroud)