Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

이 페이지는 영어로부터 커뮤니티에 의하여 번역되었습니다. MDN Web Docs에서 한국 커뮤니티에 가입하여 자세히 알아보세요.

encodeURI()

BaselineWidely available

encodeURI() 함수는URI의 특정한 문자를 UTF-8로 인코딩해 하나, 둘, 셋, 혹은 네 개의 연속된 이스케이프 문자로 나타냅니다. (두 개의 대리 문자로 이루어진 문자만 이스케이프 문자 네 개로 변환됩니다.)

시도해 보기

const uri = "https://mozilla.org/?x=шеллы";const encoded = encodeURI(uri);console.log(encoded);// Expected output: "https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B"try {  console.log(decodeURI(encoded));  // Expected output: "https://mozilla.org/?x=шеллы"} catch (e) {  // Catches a malformed URI  console.error(e);}

구문

js
encodeURI(URI);

매개변수

URI

완전한 URI.

반환 값

주어진 문자열을 URI로서 인코딩한 새로운 문자열.

설명

encodeURI() 함수는 URI에서 특별한 뜻을 가진 문자(예약 문자)는 인코딩 하지 않습니다. 아래 예제는 "URI 도식"이 포함할 수 있는 모든 부분을 담고 있습니다. 일부 문자가 어떤 방식으로 특별한 의미를 주입하고 있는지 잘 살펴보세요.

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

따라서encodeURI()는 완전한 URI를 형성하는데 필요한 문자는 인코딩하지 않습니다. 또한, 예약된 목적을 가지지는 않지만 URI가 그대로 포함할 수 있는 몇 가지 문자("비예약 표식")도 인코딩하지 않습니다. (RFC 2396을 참고하세요)

encodeURI() 는 다음 문자를제외한 문자를 이스케이프 합니다.

이스케이프 하지 않는 문자:    A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

encodeURI()encodeURIComponent()의 차이는 다음과 같습니다.

js
var set1 = ";,/?:@&=+$#"; // 예약 문자var set2 = "-_.!~*'()"; // 비예약 표식var 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%23console.log(encodeURIComponent(set2)); // -_.!~*'()console.log(encodeURIComponent(set3)); // ABC%20abc%20123 (공백은 %20으로 인코딩)

encodeURI() 혼자서는XMLHttpRequest 등이 사용할, 적합한 HTTPGETPOST 요청을 구성할 수 없습니다.GETPOST에서 특별한 문자로 취급하는"&","+","="를 인코딩 하지 않기 때문입니다. 그러나encodeURIComponent()는 저 세 문자도 인코딩 대상에 포함합니다.

상위-하위 쌍을 이루지 않은 단일 대리 문자를 인코딩 시도하면URIError가 발생합니다.

js
// high-low pair okconsole.log(encodeURIComponent("\uD800\uDFFF"));// lone high surrogate throws "URIError: malformed URI sequence"console.log(encodeURIComponent("\uD800"));// lone low surrogate throws "URIError: malformed URI sequence"console.log(encodeURIComponent("\uDFFF"));

또한, URL의 보다 최신 RFC인RFC3986을 따르고자 한다면, 대괄호가IPv6 지원을 위해 추가로 예약됨에 따라encodeURI()가 인코딩하지 않으므로 URL을 생성할 때 주의해야 합니다. 다음 예시 코드가 도움이 될 수도 있습니다.

js
function fixedEncodeURI(str) {  return encodeURI(str).replace(/%5B/g, "[").replace(/%5D/g, "]");}

명세

Specification
ECMAScript® 2026 Language Specification
# sec-encodeuri-uri

브라우저 호환성

같이 보기

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp