带有反斜杠的JavaScript encodeURIComponent

Kev*_*ith 5 javascript encoding

w3schools说以下关于encodeURIComponent功能:

此功能对特殊字符进行编码.另外,它编码以下字符:, / ? : @ & = + $ #.

这是否意味着它不能编码反斜杠(\)?

zzz*_*Bov 10

此功能对特殊字符进行编码.此外,它编码以下字符:, / ? : @ & = + $ # .

关于"特殊字符"是什么,这个定义含糊不清.这听起来像encodeURI和之间的比较encodeURIComponent.双方将正确逃生\%5C,所以你不必担心反斜杠.

encodeURI 将保留列出的字符,因为假设整个URI正在编码:

encodeURI('http://example.com/foo bar/baz.html');
//produces "http://example.com/foo%20bar/baz.html"
Run Code Online (Sandbox Code Playgroud)

encodeURIComponent将转义所有内容,因为假设该字符串将用作查询字符串的一部分:

'http://example.com?foo=' + encodeURIComponent('http://example.com/fizz/buzz.html');
//produces "http://example.com?foo=http%3A%2F%2Fexample.com%2Ffizz%2Fbuzz.html"
Run Code Online (Sandbox Code Playgroud)