| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent | [Back] [Original] |
Get to know MDN better
// 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)
URI
encodeURIComponent()
encodeURIComponent encodeURI()
AZ az 09 - _ . ! ~ * ' ( )
encodeURI() encodeURIComponent() encodeURIComponent() & / Jack & Jill encodeURIComponent()
application/x-www-form-urlencoded + encodeURIComponent() %20 +
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 !, ', (, ), * URI IPv6 URI [ ] RFC3986 encodeURI encodeURI()
function encodeRFC3986URIComponent(str) {
return encodeURIComponent(str).replace(
/[!'()*]/g,
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
);
}
//
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()
| ECMAScript 2027 LanguageSpecification # sec-encodeuricomponent-uricomponent |
| Web Proxy Viewer | New URL | Original Page |