[英]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.