javascript url解码不起作用

问题描述:

我的字符串是;javascript url解码不起作用

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; 
} 

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

+0

见http://*.com/questions/3803716/how-can-i-decode-a-url-with-jquery。 –

+0

这是错误的问题,你的问题是如何生成这些字符串,空格应该是'%20',而不是'+'或者甚至'%2b' – Esailija

对我来说,以下工作:

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

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