前回 はURLエンコーディングを説明したので今回はデコードについて調べた結果を書こうと思います。
decodeURI
encodeURI、もしくはそれに類似したルーチンによって作成された統一リソース識別子 (Uniform Resource Identifier (URI)) をデコードします。
encodeURIComponent
encodeURIComponent、もしくはそれに類似したルーチンによって作成された 統一資源識別子 (Uniform Resource Identifier (URI)) の要素をデコードします。
decodeURIComponent() - JavaScript | MDN
基本的にencodeURIでエンコードした結果をデコードする際はdecodeURIを、encodeURIComponentでエンコードした結果をデコードする際にはdecodeURIComponentを使用します。
ただし、encodeURIComponentでエンコードできない文字を含むすべての値をデコードしたい場合はそれらの値をデコードする処理も書かないといけません。
例えば、個別に対応するのであれば、
function decode (str) { return decodeURIComponent(str).replace(/%2d/g, "-") .replace(/%0d/g, "_") .replace(/%0a_/g, ".") .replace(/\%21/g, "!") .replace(/%e2%80%be/g, "~") .replace(/\%2A/g, "*") .replace(/\%27/g, "'") .replace(/\%28/g, "(") .replace(/\%29/g, ")"); }
こんな感じです。