[英]Proper way of encoding a java script string
我已使用Java脚本的转义功能对Java脚本字符串进行编码。 但是结果似乎很奇怪,其中包含以下字符:
%3CBitte%20w%E4hlen%3E
…对于字符串:
Bitte wählen
…这是德语句子。 我需要输出为Bitte wählen
。
以下代码可以帮助您清楚地理解
var temp = escape(String(<Bitte wählen>));
你的意思还不清楚。
要转义JavaScript中的任何字符,请阅读JavaScript字符转义序列 ,或仅使用mothereff.in/js-escapes 。 该页面告诉您,在JavaScript中, 'Bitte wählen'
可以写为'Bitte w\\xe4hlen'
,甚至是'\\x42\\x69\\x74\\x74\\x65\\x20\\x77\\xe4\\x68\\x6c\\x65\\x6e'
。
function URLDecode (encodedString) {
var output = encodedString;
var binVal, thisString;
var myregexp = /(%[^%]{2})/;
while ((match = myregexp.exec(output)) != null
&& match.length > 1
&& match[1] != '') {
binVal = parseInt(match[1].substr(1),16);
thisString = String.fromCharCode(binVal);
output = output.replace(match[1], thisString);
}
return output;
}
alert(URLDecode("%3CBitte%20w%E4hlen%3E")); // <Bitte wählen>
(代码取自此处 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.