你什么时候应该使用escape来代替encodeURI / encodeURIComponent?

当编码查询string发送到Web服务器 – 你什么时候使用escape()以及什么时候使用encodeURI()encodeURIComponent()

使用转义:

 escape("% +&="); 

要么

使用encodeURI()/ encodeURIComponent()

 encodeURI("http://www.google.com?var1=value1&var2=value2"); encodeURIComponent("var1=value1&var2=value2"); 

逃逸()

特殊字符的编码除了:* * _ + – 。/

代码单元值为0xFF或更小的字符的hexforms是两位数的转义序列:%xx。 对于代码单元较大的字符,使用四位数格式%uxxxx。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/escape

是encodeURI()

当你想要一个工作的URL时使用encodeURI。 拨打这个电话:

 encodeURI("http://www.example.org/a file with spaces.html") 

要得到:

 http://www.example.org/a%20file%20with%20spaces.html 

不要调用encodeURIComponent,因为它会销毁URL并返回

 http%3A%2F%2Fwww.example.org%2Fa%20file%20with%20spaces.html 

encodeURIComponent方法()

如果要编码URL参数的值,请使用encodeURIComponent。

 var p1 = encodeURIComponent("http://example.org/?a=12&b=55") 

然后你可以创build你需要的URL:

 var url = "http://example.net/?param1=" + p1 + "&param2=99"; 

你会得到这个完整的url:

http://example.net/?param1=http%3A%2F%2Fexample.org%2F%Ffa%3D12%26b%3D55&param2=99

请注意,encodeURIComponent不会转义字符。 一个常见的错误是使用它来创buildhtml属性,如href='MyUrl' ,这可能会遭受注入漏洞。 如果你是从string构造HTML,可以使用"而不是'作为属性引用,或者添加一个额外的编码层( '可以被编码为%27)。

有关这种types的编码的更多信息,你可以检查: http : //en.wikipedia.org/wiki/Percent-encoding

encodeURI()encodeURIComponent()之间的区别恰恰是由encodeURIComponent编码的11个字符,而不是encodeURI:

表与encodeURI和encodeURIComponent之间的十个区别

我使用以下代码轻松地在Google Chrome中使用console.table生成此表格:

 var arr = []; for(var i=0;i<256;i++) { var char=String.fromCharCode(i); if(encodeURI(char)!==encodeURIComponent(char)) { arr.push({ character:char, encodeURI:encodeURI(char), encodeURIComponent:encodeURIComponent(char) }); } } console.table(arr); 

我发现这篇文章启发: Javascript疯狂:查询stringparsing

当我试图解释为什么decodeURIComponent不能正确解码“+”时,我发现它。 这里是一个摘录:

 String: "A + B" Expected Query String Encoding: "A+%2B+B" escape("A + B") = "A%20+%20B" Wrong! encodeURI("A + B") = "A%20+%20B" Wrong! encodeURIComponent("A + B") = "A%20%2B%20B" Acceptable, but strange Encoded String: "A+%2B+B" Expected Decoding: "A + B" unescape("A+%2B+B") = "A+++B" Wrong! decodeURI("A+%2B+B") = "A+++B" Wrong! decodeURIComponent("A+%2B+B") = "A+++B" Wrong! 

encodeURIComponent不编码-_.!~*'() ,导致在将数据发布到xmlstring中的问题。

例如:
<xml><text x="100" y="150" value="It's a value with single quote" /> </xml>

一般与encodeURI转义
%3Cxml%3E%3Ctext%20x=%22100%22%20y=%22150%22%20value=%22It's%20a%20value%20with%20single%20quote%22%20/%3E%20%3C/xml%3E

你可以看到,单引号没有编码。 为了解决问题,我创build了两个函数来解决我的项目中的问题,对于编码URL:

 function encodeData(s:String):String{ return encodeURIComponent(s).replace(/\-/g, "%2D").replace(/\_/g, "%5F").replace(/\./g, "%2E").replace(/\!/g, "%21").replace(/\~/g, "%7E").replace(/\*/g, "%2A").replace(/\'/g, "%27").replace(/\(/g, "%28").replace(/\)/g, "%29"); } 

对于解码url:

 function decodeData(s:String):String{ try{ return decodeURIComponent(s.replace(/\%2D/g, "-").replace(/\%5F/g, "_").replace(/\%2E/g, ".").replace(/\%21/g, "!").replace(/\%7E/g, "~").replace(/\%2A/g, "*").replace(/\%27/g, "'").replace(/\%28/g, "(").replace(/\%29/g, ")")); }catch (e:Error) { } return ""; } 

encodeURI() – escape()函数用于javascript转义,而不是HTTP。

小比较表Java vs JavaScript vs. PHP。

 1. Java URLEncoder.encode (using UTF8 charset) 2. JavaScript encodeURIComponent 3. JavaScript escape 4. PHP urlencode 5. PHP rawurlencode char JAVA JavaScript --PHP--- [ ] + %20 %20 + %20 [!] %21 ! %21 %21 %21 [*] * * * %2A %2A ['] %27 ' %27 %27 %27 [(] %28 ( %28 %28 %28 [)] %29 ) %29 %29 %29 [;] %3B %3B %3B %3B %3B [:] %3A %3A %3A %3A %3A [@] %40 %40 @ %40 %40 [&] %26 %26 %26 %26 %26 [=] %3D %3D %3D %3D %3D [+] %2B %2B + %2B %2B [$] %24 %24 %24 %24 %24 [,] %2C %2C %2C %2C %2C [/] %2F %2F / %2F %2F [?] %3F %3F %3F %3F %3F [#] %23 %23 %23 %23 %23 [[] %5B %5B %5B %5B %5B []] %5D %5D %5D %5D %5D ---------------------------------------- [~] %7E ~ %7E %7E ~ [-] - - - - - [_] _ _ _ _ _ [%] %25 %25 %25 %25 %25 [\] %5C %5C %5C %5C %5C ---------------------------------------- char -JAVA- --JavaScript-- -----PHP------ [ä] %C3%A4 %C3%A4 %E4 %C3%A4 %C3%A4 [ф] %D1%84 %D1%84 %u0444 %D1%84 %D1%84 

另外请记住,他们都编码不同的字符集,并select一个你需要适当的。 encodeURI()对encodeURIComponent()进行编码的字符数less于对escapen()进行编码的字符数(与dannyp的点数不同)。

我build议不要使用这些方法之一。 写你自己的function,做正确的事情。

MDN已经给出了一个很好的例子,如下所示的url编码。

 var fileName = 'my file(2).txt'; var header = "Content-Disposition: attachment; filename*=UTF-8''" + encodeRFC5987ValueChars(fileName); console.log(header); // logs "Content-Disposition: attachment; filename*=UTF-8''my%20file%282%29.txt" function encodeRFC5987ValueChars (str) { return encodeURIComponent(str). // Note that although RFC3986 reserves "!", RFC5987 does not, // so we do not need to escape it replace(/['()]/g, escape). // ie, %27 %28 %29 replace(/\*/g, '%2A'). // The following are not required for percent-encoding per RFC5987, // so we can allow for a little better readability over the wire: |`^ replace(/%(?:7C|60|5E)/g, unescape); } 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

我发现,即使在处理好各种用途和能力之后,对各种方法进行试验也是一个很好的理智检查。

为此,我发现这个网站非常有用,可以证实我怀疑我正在做一些适当的事情。 它也被certificate是有用的解码一个encodeURIComponent的string,这可能是相当具有挑战性的解释。 一个伟大的书签有:

http://www.the-art-of-web.com/javascript/escape/

为了编码的目的,javascript给了三个内置函数 –

  1. escape() – 不编码@*/+这个方法在ECMA 3之后被弃用,所以应该避免。

  2. encodeURI() – 不编码~!@#$&*()=:/,;?+'它假定URI是一个完整的URI,所以不编码在URI中有特殊意义的保留字符。 当意图是转换完整的URL而不是一些特殊的URL段时使用这个方法。 示例 – encodeURI('http://stackoverflow.com'); 将给予 – http://stackoverflow.com

  3. encodeURIComponent() - _ . ! ~ * ' ( )不编码- _ . ! ~ * ' ( ) - _ . ! ~ * ' ( ) - _ . ! ~ * ' ( )该函数通过用代表字符的UTF-8编码的一个,两个,三个或四个转义序列replace某些字符的每个实例来编码统一资源标识符(URI)组件。 这个方法应该用来转换URL的一个组件。 例如,一些用户input需要追加例子 – encodeURI('http://stackoverflow.com'); 会给 – http%3A%2F%2Fstackoverflow.com

所有这些编码都是以UTF 8格式执行的,即字符将以UTF-8格式转换。

encodeURIComponent与encodeURI的不同之处在于它对encodeURI的保留字符和数字符号编码

我有这个function…

 var escapeURIparam = function(url) { if (encodeURIComponent) url = encodeURIComponent(url); else if (encodeURI) url = encodeURI(url); else url = escape(url); url = url.replace(/\+/g, '%2B'); // Force the replacement of "+" return url; }; 

接受的答案是好的。 最后一部分:

请注意,encodeURIComponent不会转义字符。 一个常见的错误是使用它来创buildhtml属性,如href ='MyUrl',这可能会遭受注入漏洞。 如果你是从string构造HTML,可以使用“而不是”作为属性引用,或者添加一个额外的编码层('可以被编码为%27)。

如果你想保持安全, 编码未保留字符的百分比也应该被编码。

你可以使用这个方法来转义他们(来源Mozilla )

 function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); } // fixedEncodeURIComponent("'") --> "%27"