encodeURIComponent不编码字符有71个: ! ' ( ) * - . _ ~ 0-9 a-z A-Z 我想把剩下的71个字符也编码,比如var c = "我没有CNY100万";d1 = encodeURIComponent(c); //得到 %E6%88%91%E6%B2%A1%E6%9C%89CNY100%E4%B8%87//里面的 CNY100 并没有被 编码。//我想得到的是 %E6%88%91%E6%B2%A1%E6%9C%89%43%4E%59%31%30%30%E4%B8%87d2 = xxxxx(c); //我要的就是这个函数
function xxxxxx(s) {
return encodeURIComponent(s).replace(/(%.{2})|([^%])/g, function($0, $1, $2) {
return $1 || "%" + ("0" + $2.charCodeAt(0).toString(16)).slice(-2);
});
}