特殊字符替换为空格

Special character replace with space

本文关键字:空格 替换 特殊字符      更新时间:2023-09-26

我正在传递变量 true url 和空格会自动替换为浏览器的"+"或"%20"。重定向页面后,我想正确拆分这个词。

例如:-http://localhost:8008/Login.aspx?stid=Are 你快乐

加载登录页面后,这会自动转换,

http://localhost:8008/Login.aspx?stid=Are+You+Happy

http://localhost:8008/Login.aspx?stid=Are%20You%20Happy

加载页面后如何获取此 stid 变量,

你快乐吗

有没有办法使用 javascript 或 jquery 来做到这一点?

当我发送参数时,我想要相同的结果,

http://localhost:8008/Login.aspx?stid=Are+You+Happy

我想要的结果也应该像下面一样。

是+你+快乐

这两个功能都可以由javascript或jquery完成吗?

//decode function
function decode(encodedStr) {
    return decodeURIComponent(encodedStr.replace(/'+/g,  " "));
}

//encode function
function encode(unencoded ) {
    return encodeURIComponent(unencoded).replace(/'/g,"%27").replace(/"/g,"%22");   
}


你也可以使用 JQuery.params((:
var myObject = {
    stid: 'You are happy',
    b: [ 1, 2, 3 ],
    .......
};
var recursiveEncoded = $.param( myObject );
var recursiveDecoded = decodeURIComponent( $.param( myObject ) );
alert( recursiveEncoded );
alert( recursiveDecoded );

这应该可以解决问题

var url = "http://localhost:8008/Login.aspx?stid=Are%20You%20Happy",
    decoded_url = decodeURIComponent(url.replace(/'+/g,  " "));  
    
alert(decoded_url.split('stid=')[1]);