简体   繁体   English

JavaScript中转义(字符串)的替代方法

[英]Alternatives to escape(string) in JavaScript

When changing to TypeScript I'm not allowed to use escape(string) anymore because it's deprecated . 当更改为TypeScript时,因为已弃用 ,所以不再允许使用escape(string)。 The reason I still use it is because the alternatives encodeURI and encodeURIComponent gives a different result. 我仍然使用它的原因是因为替代方法encodeURI和encodeURIComponent给出了不同的结果。

 var s = "Å" alert(escape(s)); alert(encodeURI(s)) alert(encodeURIComponent(s)) 

I don't use this for URLs, but for a CSV export. 我不是将其用于URL,而是用于CSV导出。

Other alternatives that will give me the same result at escape(string) used to provide? 其他选择是否会给我提供与使用escape(string)相同的结果?

In EcmaScript spec there is algorithm: EcmaScript规范中,有以下算法:

  1. Call ToString(string). 调用ToString(string)。
  2. Compute the number of characters in Result(1). 计算Result(1)中的字符数。
  3. Let R be the empty string. 令R为空字符串。
  4. Let k be 0. 令k为0。
  5. If k equals Result(2), return R. 如果k等于Result(2),则返回R。
  6. Get the character at position k within Result(1). 获取Result(1)中位置k处的字符。
  7. If Result(6) is one of the 69 nonblank ASCII characters ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 @*_+-./, go to step 14. 如果Result(6)是69个非空ASCII字符ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 @ * _ +-。/之一,请转到步骤14。
  8. Compute the 16-bit unsigned integer that is the Unicode character encoding of Result(6). 计算16位无符号整数,它是Result(6)的Unicode字符编码。
  9. If Result(8), is less than 256, go to step 12. 如果Result(8)小于256,请转到步骤12。
  10. Let S be a string containing six characters “%uwxyz” where wxyz are four hexadecimal digits encoding the value of Result(8). 令S为包含六个字符“%uwxyz”的字符串,其中wxyz是编码Result(8)值的四个十六进制数字。
  11. Go to step 15. 转到步骤15。
  12. Let S be a string containing three characters “%xy” where xy are two hexadecimal digits encoding the value of Result(8). 令S为包含三个字符“%xy”的字符串,其中xy是编码Result(8)值的两个十六进制数字。
  13. Go to step 15. 转到步骤15。
  14. Let S be a string containing the single character Result(6). 令S为包含单个字符Result(6)的字符串。
  15. Let R be a new string value computed by concatenating the previous value of R and S. 令R为通过串联R和S的先前值而计算出的新字符串值。
  16. Increase k by 1. 将k增加1。
  17. Go to step 5. 转到步骤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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM