javascript url解码不起作用

时间:2013-08-02 12:38:26

标签: javascript urldecode

我的字符串是;

var str = "Mehmet%2bAli%2b%c3%96zcan";

我想得到字符串;

var strDecoded = "Mehmet Ali Özcan";

我尝试了以下所有内容;

strDecoded = decodeURIComponent(str); // Fails;
strDecoded = decodeURIComponent((str + '').replace(/\+/g, '%20')); // Fails
strDecoded = _decodeURI(str); // Fails


function _decodeURI(str) {
  str = decodeURI(str);
  str = str.replace(/%27/g, "'");
  return str;
}

我还能做些什么来获得正确的字符串?任何想法?

2 个答案:

答案 0 :(得分:3)

以下适用于我:

decodeURIComponent("Mehmet%2bAli%2b%c3%96zcan").replace(/\++/g, ' ');

答案 1 :(得分:1)

decodeURIComponent(str.replace(/%2b/g, '%20'));