decodeURIComponent()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
decodeURIComponent()
関数は、encodeURIComponent()
関数あるいは同様のルーチンによって事前に作成された URI (Uniform Resource Identifier; 統一資源識別子) の構成要素をデコードします。
試してみましょう
function containsEncodedComponents(x) { // ie ?,=,&,/ etc return decodeURI(x) !== decodeURIComponent(x);}console.log(containsEncodedComponents("%3Fx%3Dtest")); // ?x=test// Expected output: trueconsole.log(containsEncodedComponents("%D1%88%D0%B5%D0%BB%D0%BB%D1%8B")); // шеллы// Expected output: false
構文
decodeURIComponent(encodedURI)
引数
encodedURI
エンコードされた URI の構成要素です。
返値
エンコードされた統一資源識別子 (URI) の構成要素をデコードしたものを表す新しい文字列です。
例外
不正に利用された場合はURIError
("malformed URI sequence") 例外が発生します。
解説
エンコードされた URI の構成要素のエスケープシーケンスを、それぞれが表す文字に置き換えます。
例
キリル文字の URL の構成要素をデコード
js
decodeURIComponent("JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B");// "JavaScript_шеллы"
エラーの捕捉
js
try { var a = decodeURIComponent("%E0%A4%A");} catch (e) { console.error(e);}// URIError: malformed URI sequence
URL からのクエリーパラメータのデコード
decodeURIComponent は、URL からのクエリーパラメータを解析するために直接使用することはできません。少し準備が必要です。
js
function decodeQueryParam(p) { return decodeURIComponent(p.replace(/\+/g, " "));}decodeQueryParam("search+query%20%28correct%29");// 'search query (correct)'
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-decodeuricomponent-encodeduricomponent |