[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent [Back]  [Original]

encodeURIComponent() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

encodeURIComponent()

Baseline

20157

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"

js
encodeURIComponent(uriComponent)

uriComponent

URI

URI

URIError

uriComponent

encodeURIComponent()

encodeURIComponent encodeURI()

AZ az 09 - _ . ! ~ * ' ( )

encodeURI() encodeURIComponent() encodeURIComponent() & / Jack & Jill encodeURIComponent()

application/x-www-form-urlencoded + encodeURIComponent() %20 +

Content-Disposition

Content-Disposition Link (UTF-8 ) UTF-8

js
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()

js
function encodeRFC3986URIComponent(str) {
  return encodeURIComponent(str).replace(
    /[!'()*]/g,
    (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
  );
}

URIError

js
// 
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