每個 ASCII 字符的百分比編碼形式、RFC 3986 保留字符以及開發人員的實用編碼規則。
| Char | Decimal | Hex | URL Encoded | Name |
|---|---|---|---|---|
| 32 | 0x20 | %20 | Space | |
| ! | 33 | 0x21 | 無需編碼 | Exclamation mark |
| " | 34 | 0x22 | %22 | Double quote |
| # | 35 | 0x23 | %23 | Hash / Number sign |
| $ | 36 | 0x24 | %24 | Dollar sign |
| % | 37 | 0x25 | %25 | Percent sign |
| & | 38 | 0x26 | %26 | Ampersand |
| ' | 39 | 0x27 | 無需編碼 | Single quote / Apostrophe |
| ( | 40 | 0x28 | 無需編碼 | Left parenthesis |
| ) | 41 | 0x29 | 無需編碼 | Right parenthesis |
| * | 42 | 0x2A | 無需編碼 | Asterisk |
| + | 43 | 0x2B | %2B | Plus sign |
| , | 44 | 0x2C | %2C | Comma |
| - | 45 | 0x2D | 無需編碼 | Hyphen / Minus |
| . | 46 | 0x2E | 無需編碼 | Period / Dot |
| / | 47 | 0x2F | %2F | Forward slash |
| 0 | 48 | 0x30 | 無需編碼 | Zero |
| 1 | 49 | 0x31 | 無需編碼 | |
| 2 | 50 | 0x32 | 無需編碼 | |
| 3 | 51 | 0x33 | 無需編碼 | |
| 4 | 52 | 0x34 | 無需編碼 | |
| 5 | 53 | 0x35 | 無需編碼 | |
| 6 | 54 | 0x36 | 無需編碼 | |
| 7 | 55 | 0x37 | 無需編碼 | |
| 8 | 56 | 0x38 | 無需編碼 | |
| 9 | 57 | 0x39 | 無需編碼 | Nine |
| : | 58 | 0x3A | %3A | Colon |
| ; | 59 | 0x3B | %3B | Semicolon |
| < | 60 | 0x3C | %3C | Less-than |
| = | 61 | 0x3D | %3D | Equals sign |
| > | 62 | 0x3E | %3E | Greater-than |
| ? | 63 | 0x3F | %3F | Question mark |
| @ | 64 | 0x40 | %40 | At sign |
| A | 65 | 0x41 | 無需編碼 | Capital A |
| B | 66 | 0x42 | 無需編碼 | |
| C | 67 | 0x43 | 無需編碼 | |
| D | 68 | 0x44 | 無需編碼 | |
| E | 69 | 0x45 | 無需編碼 | |
| F | 70 | 0x46 | 無需編碼 | |
| G | 71 | 0x47 | 無需編碼 | |
| H | 72 | 0x48 | 無需編碼 | |
| I | 73 | 0x49 | 無需編碼 | |
| J | 74 | 0x4A | 無需編碼 | |
| K | 75 | 0x4B | 無需編碼 | |
| L | 76 | 0x4C | 無需編碼 | |
| M | 77 | 0x4D | 無需編碼 | |
| N | 78 | 0x4E | 無需編碼 | |
| O | 79 | 0x4F | 無需編碼 | |
| P | 80 | 0x50 | 無需編碼 | |
| Q | 81 | 0x51 | 無需編碼 | |
| R | 82 | 0x52 | 無需編碼 | |
| S | 83 | 0x53 | 無需編碼 | |
| T | 84 | 0x54 | 無需編碼 | |
| U | 85 | 0x55 | 無需編碼 | |
| V | 86 | 0x56 | 無需編碼 | |
| W | 87 | 0x57 | 無需編碼 | |
| X | 88 | 0x58 | 無需編碼 | |
| Y | 89 | 0x59 | 無需編碼 | |
| Z | 90 | 0x5A | 無需編碼 | Capital Z |
| [ | 91 | 0x5B | %5B | Left square bracket |
| \ | 92 | 0x5C | %5C | Backslash |
| ] | 93 | 0x5D | %5D | Right square bracket |
| ^ | 94 | 0x5E | %5E | Caret |
| _ | 95 | 0x5F | 無需編碼 | Underscore |
| ` | 96 | 0x60 | %60 | Backtick / Grave |
| a | 97 | 0x61 | 無需編碼 | Lowercase a |
| b | 98 | 0x62 | 無需編碼 | |
| c | 99 | 0x63 | 無需編碼 | |
| d | 100 | 0x64 | 無需編碼 | |
| e | 101 | 0x65 | 無需編碼 | |
| f | 102 | 0x66 | 無需編碼 | |
| g | 103 | 0x67 | 無需編碼 | |
| h | 104 | 0x68 | 無需編碼 | |
| i | 105 | 0x69 | 無需編碼 | |
| j | 106 | 0x6A | 無需編碼 | |
| k | 107 | 0x6B | 無需編碼 | |
| l | 108 | 0x6C | 無需編碼 | |
| m | 109 | 0x6D | 無需編碼 | |
| n | 110 | 0x6E | 無需編碼 | |
| o | 111 | 0x6F | 無需編碼 | |
| p | 112 | 0x70 | 無需編碼 | |
| q | 113 | 0x71 | 無需編碼 | |
| r | 114 | 0x72 | 無需編碼 | |
| s | 115 | 0x73 | 無需編碼 | |
| t | 116 | 0x74 | 無需編碼 | |
| u | 117 | 0x75 | 無需編碼 | |
| v | 118 | 0x76 | 無需編碼 | |
| w | 119 | 0x77 | 無需編碼 | |
| x | 120 | 0x78 | 無需編碼 | |
| y | 121 | 0x79 | 無需編碼 | |
| z | 122 | 0x7A | 無需編碼 | Lowercase z |
| { | 123 | 0x7B | %7B | Left curly brace |
| | | 124 | 0x7C | %7C | Pipe / Vertical bar |
| } | 125 | 0x7D | %7D | Right curly brace |
| ~ | 126 | 0x7E | 無需編碼 | Tilde |
| Function | Encodes | Leaves alone | Use for |
|---|---|---|---|
| encodeURIComponent() | Everything except A-Z a-z 0-9 - _ . ! ~ * ' ( ) | Alphanumerics + - _ . ! ~ * ' ( ) | Query param values, path segments |
| encodeURI() | Unsafe chars, preserves : / ? # [ ] @ ! $ & ' ( ) * + , ; = | Most URI structural chars | Full URLs |
| escape() ✗ | Non-ASCII + some ASCII (deprecated) | @ * / + | Never — deprecated, use encodeURIComponent |
// Encode a single query param value (most common)
encodeURIComponent('hello world!') // → 'hello%20world!'
encodeURIComponent('price=100&qty=2') // → 'price%3D100%26qty%3D2'
// Build a URL with params safely
const params = new URLSearchParams({ q: 'hello world', page: '2' });
const url = 'https://example.com/search?' + params.toString();
// → https://example.com/search?q=hello+world&page=2
// Decode percent-encoded string
decodeURIComponent('hello%20world') // → 'hello world'
// Python
import urllib.parse
urllib.parse.quote('hello world') # → 'hello%20world'
urllib.parse.quote_plus('hello world') # → 'hello+world'
urllib.parse.urlencode({'q': 'hello world'}) # → 'q=hello+world'URL 編碼將不安全的 ASCII 字符替換為百分號 (%) 後跟兩個表示字節值的十六進制數字。例如,空格變為 %20 或 +。這確保 URL 在所有系統和協議中都有效。
RFC 3986 定義了哪些字符是「非保留」字符(A-Z、a-z、0-9、-、_、.、~),可以安全使用而無需編碼。所有其他字符 — 包括空格、<、>、#、%、{、}、|、\、^、` 和非 ASCII 字符 — 必須進行百分比編碼。
%20 是 RFC 3986 標準的空格百分比編碼,在 URL 的任何位置都有效。+ 符號表示空格是 application/x-www-form-urlencoded 格式(HTML 表單)的一部分,僅在查詢字符串中有效,在路徑中無效。
使用 encodeURIComponent() 對單個查詢參數值進行編碼:encodeURIComponent('hello world') → 'hello%20world'。使用 encodeURI() 在保留結構的同時對完整 URL 進行編碼。永遠不要使用 escape() — 它已被棄用。