对要发送到web服务器的查询字符串进行编码时-何时使用escape(),何时使用encodeURI()或encodeURIComponent():

使用转义符:

escape("% +&=");

OR

使用encodeURI()/encodeURIComponent()

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

encodeURIComponent("var1=value1&var2=value2");

当前回答

Java与JavaScript与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

其他回答

我觉得这篇文章很有启发性:Javascript疯狂:查询字符串解析

当我试图解释为什么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!

受Johann桌子的启发,我决定延长桌子。我想看看哪些ASCII字符被编码。

var ascii=“!\”#$%&'()*+,-/0123456789:;<=>?@EFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvxyz{|}~“;var编码=[];ascii.split(“”).forEach(函数(字符){var obj={char};if(char!=编码URI(char))obj.encodeURI=编码URI(字符);if(char!=encodeURIComponent(char))obj.encodeURIComponent=编码URI组件(字符);if(obj.encodeURI|| obj.encoURIComponent)编码推送(obj);});console.table(编码);

表仅显示编码字符。空单元格表示原始字符和编码字符相同。


另外,我为urlenoder()和rawurlenodes()添加了另一个表。唯一的区别似乎是空格字符的编码。

<script>
<?php
$ascii = str_split(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", 1);
$encoded = [];
foreach ($ascii as $char) {
    $obj = ["char" => $char];
    if ($char != urlencode($char))
        $obj["urlencode"] = urlencode($char);
    if ($char != rawurlencode($char))
        $obj["rawurlencode"] = rawurlencode($char);
    if (isset($obj["rawurlencode"]) || isset($obj["rawurlencode"]))
        $encoded[] = $obj;
}
echo "var encoded = " . json_encode($encoded) . ";";
?>
console.table(encoded);
</script>

我有这个功能。。。

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;
};

@johann echavarria答案的现代改写:

控制台日志(阵列(256).fill().map((忽略,i)=>String.fromCharCode(i)).过滤器((字符)=>encodeURI(char)!==encodeURIComponent(字符)? {字符:char,encodeURI:encodeURI(char),encodeURIComponent:encodeURIComponent(char)}:错误))

或者,如果可以使用表,请将console.log替换为console.table(以获得更漂亮的输出)。

为了编码,javascript提供了三个内置函数-

escape()-不编码@*/+此方法在ECMA 3之后被弃用,因此应避免使用。encodeURI()-不编码~@#$&*()=:/,;?+'它假定URI是一个完整的URI,因此不会对URI中具有特殊含义的保留字符进行编码。当意图转换完整的URL而不是URL的某个特殊段时,使用此方法。示例-encodeURI('http://stackoverflow.com');将给出-http://stackoverflow.comencodeURIComponent()-不编码-__!~*'( )此函数通过用表示字符UTF-8编码的一个、两个、三个或四个转义序列替换某些字符的每个实例来编码统一资源标识符(URI)组件。此方法应用于转换URL的组件。例如,需要附加一些用户输入示例-encodeURIComponent('http://stackoverflow.com');将提供-http%3A%2F%2Stackoverflow.com

所有这些编码都在UTF 8中执行,即字符将转换为UTF-8格式。

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