admin管理员组文章数量:1406937
When changing to TypeScript I'm not allowed to use escape(string) anymore because it's deprecated. The reason I still use it is that the alternatives encodeURI and encodeURIComponent give a different results.
var s = "Å"
console.log(escape(s));
console.log(encodeURI(s));
console.log(encodeURIComponent(s));
When changing to TypeScript I'm not allowed to use escape(string) anymore because it's deprecated. The reason I still use it is that the alternatives encodeURI and encodeURIComponent give a different results.
var s = "Å"
console.log(escape(s));
console.log(encodeURI(s));
console.log(encodeURIComponent(s));
I don't use this for URLs, but for a CSV export.
What are other alternatives that will give me the same result as escape(string)
?
- What is purpose of escaping string? – guest271314 Commented May 18, 2016 at 14:18
- You can read EcmaScript specification section 15.1.2.4 where you can find algorithm for escape and implment the function yourself. – jcubic Commented May 18, 2016 at 14:29
- Do you depend on another piece of software that requires this precise format? Because the reason the two results are so different is that they are based on two different string encodings, which means that you may have further problems down the line when loading the CSV file and that you have to choose something that is future-proof and can be easily decoded (And CSV is totally agnostic on that regard). – raphv Commented May 18, 2016 at 14:33
- 2 I use this for exporting a datatable (json) to CSV format and open it in Excel. The export contains Norwegian charachters (like æ,ø,å,Æ,Ø,Å) which don't render correct in Excel unless I use the escape(string) function... – Engern Commented May 18, 2016 at 15:26
1 Answer
Reset to default 8In EcmaScript spec there is algorithm:
- Call ToString(string).
- Compute the number of characters in Result(1).
- Let R be the empty string.
- Let k be 0.
- If k equals Result(2), return R.
- Get the character at position k within Result(1).
- If Result(6) is one of the 69 nonblank ASCII characters ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 @*_+-./, go to step 14.
- Compute the 16-bit unsigned integer that is the Unicode character encoding of Result(6).
- If Result(8), is less than 256, go to step 12.
- Let S be a string containing six characters “%uwxyz” where wxyz are four hexadecimal digits encoding the value of Result(8).
- Go to step 15.
- Let S be a string containing three characters “%xy” where xy are two hexadecimal digits encoding the value of Result(8).
- Go to step 15.
- Let S be a string containing the single character Result(6).
- Let R be a new string value puted by concatenating the previous value of R and S.
- Increase k by 1.
- Go to step 5.
which can be coded like this:
(function(global) {
var allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./,';
global.escapeString = function(str) {
str = str.toString();
var len = str.length, R = '', k = 0, S, chr, ord;
while(k < len) {
chr = str[k];
if (allowed.indexOf(chr) != -1) {
S = chr;
} else {
ord = str.charCodeAt(k);
if (ord < 256) {
S = '%' + ("00" + ord.toString(16)).toUpperCase().slice(-2);
} else {
S = '%u' + ("0000" + ord.toString(16)).toUpperCase().slice(-4);
}
}
R += S;
k++;
}
return R;
};
})(typeof window == 'undefined' ? global : window);
本文标签: Alternatives to escape(string) in JavaScriptStack Overflow
版权声明:本文标题:Alternatives to escape(string) in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745051880a2639704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论