본문 바로가기

[IT/Programming]/HTML related

Encode/Unescape and Decode/Escape URI Component

반응형
# Encode/Unescape and Decode/Escape URI Component http 주소창에 space 나 한글, 특수문자 같은 것들이 낑겨 있을 경우, 여러모로 에러가 날 가능성이 높아져서 보통 브라우저들이 이런 문자들은 '%AB' 같은 8 bit (?) 어쩌구로 변환시켜 버린다. Javascript 에서 기본적으로 이런것들을 처리하는 encodeURIComponent, decodeURIComponent 함수들을 제공한다. 뭐 쓰는 사람은 알아서 공부해서 쓸테니... 인터넷 돌아다니다가 이상한 주소가 실제 어떤 주소인지 궁금할때 이용하시라고 (+내가 쓸라고) 만들어 봄. ## PH
  • 2023-10-25 : Update. m.escapeXXX... added.
## TOC ## Decode URI Component 한글같은 경우 3 byte 가 한묶음이라, 제대로 조합이 안맞으면 decode error 남. ```[#text-decode-result.scrollable] text-decode-result ```/ ## Encode URI Component ```[#text-encode-result.scrollable] text-encode-result ```/ ## Codes ### Escape codes manually coded. ```[.linenums.lang-js] //////////////////////////////////////////////////// // Escape and Unescape HTML string. //////////////////////////////////////////////////// m.escapeHTML=function (str) { if (!str||str.constructor!==String) { return ""; } return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;'); }; m.escapeOnlyTag=function (str) { if (!str||str.constructor!==String) { return ""; } return str.replace(/</g,'&lt;').replace(/>/g,'&gt;'); }; m.unescapeHTML=function (str) { if (!str||str.constructor!==String) { return ""; } return str.replace(/&gt;/g,'>').replace(/&lt;/g,'<').replace(/&amp;/g,'&'); }; m.escapeAMP=function (str) { if (!str||str.constructor!==String) { return ""; } return str.replace(/%/g,'%25').replace(/&/g,'%26').replace(/#/g,'%23'); }; m.unescapeAMP=function (str) { if (!str||str.constructor!==String) { return ""; } return str.replace(/%23/g,'#').replace(/%26/g,'&').replace(/%25/g,'%'); }; ```/ ### Using existing functions. ```[#pre-code.scrollable.lang-js] ```/ ## RRA
  1. w3schools - JavaScript Global Reference
반응형