
URI Component를 DecodeURIComponent으로 디코딩하는 방법에 대해 설명드리겠습니다.
DecodeURIComponent 함수는 URL Component 내에 있는 URL 인코딩을 디코딩하는 함수입니다. 이 함수를 사용하면 URL Component 내의 특수 문자나 기호가 디코딩되어 원래의 문자로 변환됩니다.
이 함수를 사용할 때 주의할 점은 URL Component 내의 인코딩된 문자를 디코딩하는 것이므로, URL Component 내의 인코딩된 문자가 없을 경우 디코딩된 결과가 원래의 문자와 동일합니다.
DecodeURIComponent 함수를 사용하여 URL parameter를 디코딩할 수 있습니다. 예를 들어, URL parameter로 "name=John%20Doe"이 넘어오면, DecodeURIComponent 함수를 사용하여 디코딩하면 "name=John Doe"이 됩니다.
디코딩된 결과를 사용할 때는 주의할 점이 있습니다. 디코딩된 결과는 문자열로 반환되므로, 이 문자열을 다른 변수나 객체에 할당하거나, 다른 함수에 전달할 때 주의해야 합니다.
예를 들어, 디코딩된 결과를 사용하여 URL parameter를 객체에 할당할 때는 다음과 같이 할 수 있습니다.
#hostingforum.kr
javascript
const uriComponent = "name=John%20Doe";
const decodedComponent = decodeURIComponent(uriComponent);
const params = {};
const pairs = decodedComponent.split("&");
pairs.forEach(pair => {
const [key, value] = pair.split("=");
params[key] = value;
});
console.log(params); // { name: "John Doe" }
이러한 예제를 통해 URI Component를 DecodeURIComponent으로 디코딩하는 방법과 디코딩된 결과를 사용하는 방법을 이해할 수 있습니다.
2025-06-19 15:55