encodeURIComponent()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015å¹´7æ.
encodeURIComponent() 颿°ã¯ URI ããç¹å®ã®æåã®åã¤ã³ã¹ã¿ã³ã¹ãããã®æåã® UTF-8 ã¨ã³ã³ã¼ãæ¹å¼ã表ã 1 ã¤ãã 4 ã¤ã®ã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ã§ç½®ãæãããã¨ã§ã¨ã³ã³ã¼ããã¾ã (2 ã¤ã®ãµãã²ã¼ãæåã§æ§æãããæåã®å ´å㯠4 ã¤ã®ã¨ã¹ã±ã¼ãã·ã¼ã±ã³ã¹ã«ãªãã¾ã)ã encodeURI() ã¨æ¯è¼ããã¨ããã®é¢æ°ã¯ URI æ§æã®ä¸é¨ãå«ãããå¤ãã®æåãã¨ã³ã³ã¼ããã¾ãã
試ãã¦ã¿ã¾ããã
// Encodes characters such as ?,=,/,&,:
console.log(`?x=${encodeURIComponent("test?")}`);
// äºæ³ãããçµæ: "?x=test%3F"
console.log(`?x=${encodeURIComponent("ÑеллÑ")}`);
// äºæ³ãããçµæ: "?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"
æ§æ
encodeURIComponent(uriComponent)
弿°
uriComponent-
URI ã®é¨åï¼ãã¹ãã¯ã¨ãªã¼æååããã©ã°ã¡ã³ããªã©ï¼ã¨ãã¦ã¨ã³ã³ã¼ããããæååãä»ã«ãæååã«å¤æãããå¤ãããã¾ãã
è¿å¤
ä¸ããããæååã表ã URI æ§æè¦ç´ ã¨ãã¦ã¨ã³ã³ã¼ããããæ°ããæååã§ãã
ä¾å¤
URIError-
uriComponentã«å¤ç«ãµãã²ã¼ããããã¨çºçãã¾ãã
è¿å¤
encodeURIComponent() ã¯ã°ãã¼ãã«ãªãã¸ã§ã¯ãã®é¢æ°ããããã£ã§ãã
encodeURIComponent 㯠encodeURI() ã§èª¬æããã¦ããã®ã¨åãã¨ã³ã³ã¼ãã£ã³ã°ã¢ã«ã´ãªãºã ã使ç¨ãã¾ããä¸è¨ãé¤ããã¹ã¦ã®æåãã¨ã¹ã±ã¼ããã¾ãã
AâZ aâz 0â9 - _ . ! ~ * ' ( )
encodeURI() ã¨æ¯è¼ããã¨ã encodeURIComponent() ã¯ããå¤ãã®æåã»ãããã¨ã¹ã±ã¼ããã¾ãããµã¼ãã¼ã«éä¿¡ããããã©ã¼ã ã®ã¦ã¼ã¶ã¼å
¥åãã£ã¼ã«ãã«ã¯ encodeURIComponent() ã使ç¨ãã¦ãã ãããããã«ãããæååç
§ã®ãã¼ã¿å
¥åä¸ã«ä¸æ³¨æã§çæãããå¯è½æ§ã®ãã & è¨å·ããã¨ã³ã³ã¼ã/ãã³ã¼ããè¦æ±ããããã®ä»ã®æåãã¨ã³ã³ã¼ãããã¾ããä¾ãã°ãã¦ã¼ã¶ã¼ã Jack & Jill ã¨å
¥åããå ´åãencodeURIComponent() ã使ç¨ããªãã¨ãã¢ã³ããµã³ãã¯ãµã¼ãã¼ä¸ã§æ°ãããã£ã¼ã«ãã®éå§ã¨ãã¦è§£éããããã¼ã¿ã®æ´åæ§ãæãªãããããããããã¾ãã
application/x-www-form-urlencoded ã§ã¯ãã¹ãã¼ã¹ã¯ + ã«ç½®æããã¾ãããã®ãããencodeURIComponent() ã«ããç½®æã«å ã㦠%20 ã + ã«ç½®ãæãããã¨ãæã¾ããããããã¾ããã
ä¾
>Content-Disposition ã¨ãªã³ã¯ãããã¼ã®ã¨ã³ã³ã¼ãã£ã³ã°
次ã®ä¾ã¯ããµã¼ãã¼ã¬ã¹ãã³ã¹ãããã¼å¼æ°ã® Content-Disposition ã Link ã§ (UTF-8 ãããªããã¡ã¤ã«åãªã©ã«) å¿
è¦ã¨ãªãç¹å¥ãª UTF-8 ã¨ã³ã³ã¼ãã£ã³ã°ãæä¾ãã¾ãã
const fileName = "my file(2).txt";
const header = `Content-Disposition: attachment; filename*=UTF-8''${encodeRFC5987ValueChars(
fileName,
)}`;
console.log(header);
// "Content-Disposition: attachment; filename*=UTF-8''my%20file%282%29.txt"
function encodeRFC5987ValueChars(str) {
return (
encodeURIComponent(str)
// The following creates the sequences %27 %28 %29 %2A (Note that
// the valid encoding of "*" is %2A, which necessitates calling
// toUpperCase() to properly encode). Although RFC3986 reserves "!",
// RFC5987 does not, so we do not need to escape it.
.replace(
/['()*]/g,
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
)
// 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, (str, hex) =>
String.fromCharCode(parseInt(hex, 16)),
)
);
}
RFC3986 ã®ã¨ã³ã³ã¼ãã£ã³ã°
ææ°ã® RFC3986 ã§ã¯ã !, ', (, ), * ãããã¨ããããã®æåãæ£å¼ãª URI åºåãæåã¨ãã¦ä½¿ç¨ããã¦ããªãã¨ãã¦ãäºç´ãã¦ãã¾ãã IPv6 ã® URI æ§æã®ä¸é¨ã§ãã [ 㨠] ãã¨ã³ã³ã¼ããã¾ãã RFC3986 ã«æºæ ãã encodeURI ã®å®è£
ã§ã¯ãããããã¨ã¹ã±ã¼ããã¹ãã§ã¯ããã¾ããããã㯠encodeURI() ã®ä¾ã§ç¤ºããã¦ãã¾ãã
function encodeRFC3986URIComponent(str) {
return encodeURIComponent(str).replace(
/[!'()*]/g,
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
);
}
å¤ç«ãµãã²ã¼ãã®ã¨ã³ã³ã¼ãã«ããä¾å¤
ä¸ä½ã»ä¸ä½ã®ãã¢ã§ãªããµãã²ã¼ãæåãã¨ã³ã³ã¼ããããã¨ããå ´å URIError ãçºçãã¾ãã
// ä¸ä½ã»ä¸ä½ã®æ£ãããã¢
encodeURIComponent("\uD800\uDFFF"); // "%F0%90%8F%BF"
// ä¸ä½ã®ã¿ã§ãã "URIError: malformed URI sequence" ãçºç
encodeURIComponent("\uD800");
// ä¸ä½ã®ã¿ã§ãã "URIError: malformed URI sequence" ãçºç
encodeURIComponent("\uDFFF");
String.prototype.toWellFormed() ã使ç¨ãããã¨ãã§ãã¾ããããã¯å¤ç«ãµãã²ã¼ãã Unicode ç½®ææå (U+FFFD) ã«ç½®ãæãããã¨ã§ããã®ã¨ã©ã¼ãé¿ãããã¨ãã§ãã¾ããã¾ãã String.prototype.isWellFormed() ã使ç¨ãããã¨ã§ãæååã encodeURIComponent() ã«æ¸¡ãåã«ãå¤ç«ãµãã²ã¼ããå«ã¾ãã¦ãããã©ããã調ã¹ããã¨ãã§ãã¾ãã
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-encodeuricomponent-uricomponent> |