Differences between encodeURI, encodeURIComponent, and escape: lower ASCII characters (codes 1-127)

JavaScript FAQ | Strings & RegExp FAQ | URL-encoding  

The following table illustrates the differences between URL-encoding results obtained using the JavaScript functions

encodeURI
,
encodeURIComponent
, and
escape
for lower ASCII characters, i.e. character codes up to 127 (see also Upper ASCII and Unicode characters).

The

encodeURI
and
encodeURIComponent
functions are similar to
escape
, except that they leave intact some characters that
escape
encodes (e.g. apostrophe, tilde, parentheses). Additionally,
encodeURIComponent
encodes some characters (
+ / @
) that
escape
leaves intact.
chr     escape(chr)   encodeURI(chr)  encodeURIComponent(chr)
 _          _              _               _
 -          -              -               -
 .          .              .               .
 *          *              *               *
 +          +              +               %2B
 /          /              /               %2F
 @          @              @               %40
 ~          %7E            ~               ~
 !          %21            !               !
 '          %27            '               '
 (          %28            (               (
 )          %29            )               )
 #          %23            #               %23
 $          %24            $               %24
 &          %26            &               %26
 ,          %2C            ,               %2C
 :          %3A            :               %3A
 ;          %3B            ;               %3B
 =          %3D            =               %3D
 ?          %3F            ?               %3F

all other lower-ASCII characters produce identical results:

space       %20            %20             %20
 "          %22            %22             %22
 %          %25            %25             %25
 <          %3C            %3C             %3C
 >          %3E            %3E             %3E
 [          %5B            %5B             %5B
 \          %5C            %5C             %5C
 ]          %5D            %5D             %5D
 ^          %5E            %5E             %5E
 {          %7B            %7B             %7B
 |          %7C            %7C             %7C
 }          %7D            %7D             %7D
...         ...            ...             ...

 

Copyright © 1996-2018, JavascriptFAQ.net.