[英]Proper way of encoding a java script string
I have used escape function of java script to encode a java script string. 我已使用Java脚本的转义功能对Java脚本字符串进行编码。 But the result appears to be weird one which consists of the following characters: 但是结果似乎很奇怪,其中包含以下字符:
%3CBitte%20w%E4hlen%3E
…for the string: …对于字符串:
Bitte wählen
…which is a German sentence. …这是德语句子。 I need the output as Bitte wählen
. 我需要输出为Bitte wählen
。
The following code may help you understand clearly 以下代码可以帮助您清楚地理解
var temp = escape(String(<Bitte wählen>));
It's not really clear what you mean. 你的意思还不清楚。
To escape any character in JavaScript, read up on JavaScript character escape sequences , or just use mothereff.in/js-escapes . 要转义JavaScript中的任何字符,请阅读JavaScript字符转义序列 ,或仅使用mothereff.in/js-escapes 。 That page tells you that in JavaScript, 'Bitte wählen'
can be written as 'Bitte w\\xe4hlen'
or even '\\x42\\x69\\x74\\x74\\x65\\x20\\x77\\xe4\\x68\\x6c\\x65\\x6e'
. 该页面告诉您,在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>
(Code taken from here ) (代码取自此处 )
Demo: http://jsfiddle.net/karim79/nnRpn/ 演示: http : //jsfiddle.net/karim79/nnRpn/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.