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

encodeURI() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

encodeURI()

Baseline

20157

encodeURI() UTF-8 14 URI 2 4 encodeURIComponent() URI

const uri = "https://mozilla.org/?x=";
const encoded = encodeURI(uri);
console.log(encoded);
// : "https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"

try {
  console.log(decodeURI(encoded));
  // : "https://mozilla.org/?x="
} catch (e) {
  // Catches a malformed URI
  console.error(e);
}

js
encodeURI(uri)

uri

URI

URI

URIError

uri

encodeURI()

encodeURI() UTF-8 %XX 0 UTF-16 Unicode encodeURI() URIError

encodeURI()

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

; / ? : @ & = + $ , #

2 URI encodeURIComponent() encodeURI() encodeURIComponent() -.!~*'() URI RFC2396

encodeURI() URI URI

url
http://username:password@www.example.com:80/path/to/file.php?foo=316&bar=this+has+spaces#anchor

encodeURI URL URL URL URL encodeURIComponent()

js
const name = "Ben & Jerry's";

// This is bad:
const link = encodeURI(`https://example.com/?choice=${name}`); // "https://example.com/?choice=Ben%20&%20Jerry's"
console.log([...new URL(link).searchParams]); // [['choice', 'Ben '], [" Jerry's", '']

// Instead:
const link = encodeURI(
  `https://example.com/?choice=${encodeURIComponent(name)}`,
);
// "https://example.com/?choice=Ben%2520%2526%2520Jerry's"
console.log([...new URL(link).searchParams]); // [['choice', "Ben%20%26%20Jerry's"]]

encodeURI() encodeURIComponent()

encodeURI() encodeURIComponent()

js
const set1 = ";/?:@&=+$,#"; // 
const set2 = "-.!~*'()"; // 
const set3 = "ABC abc 123"; //  + 

console.log(encodeURI(set1)); // ;/?:@&=+$,#
console.log(encodeURI(set2)); // -.!~*'()
console.log(encodeURI(set3)); // ABC%20abc%20123 ( %20 )

console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24%23
console.log(encodeURIComponent(set2)); // -.!~*'()
console.log(encodeURIComponent(set3)); // ABC%20abc%20123 ( %20 )

1 URIError

js
//  OK
console.log(encodeURI("\uD800\uDFFF"));

//  "URIError: malformed URI sequence" 
console.log(encodeURI("\uD800"));

//  "URIError: malformed URI sequence" 
console.log(encodeURI("\uDFFF"));

Unicode (U+FFFD) String.prototype.toWellFormed() encodeURI() String.prototype.isWellFormed()

RFC3986

RFC3986 IPv6 URL URI !, ', (, ), * RFC3986 URL

js
function encodeRFC3986URI(str) {
  return encodeURI(str)
    .replace(/%5B/g, "[")
    .replace(/%5D/g, "]")
    .replace(
      /[!'()*]/g,
      (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
    );
}

ECMAScript 2027 LanguageSpecification
# sec-encodeuri-uri


Web Proxy Viewer  |  New URL  |  Original Page