正直今まで decodeURI と decodeURIComponent のどちらを選んでも困る経験がなかったので人間として最悪ですがずっと適当に使っていました。正月でヒマなので調べました。
- リファレンス
結論
先に結論だけまとめると関数名のとおり以下のような使い分けとなる
- URL全体をエンコード/デコードするとき: encodeURI/decodeURI
- パス・クエリパラメータをエンコード/デコードするとき: encodeURIComponent/decodeURIComponent
挙動の違い
encodeURIComponent で URI 全体をエンコードすると URI の形式を破壊してしまう。ただそれだけ。
> encodeURIComponent("http://example.com")
'http%3A%2F%2Fexample.com'
> encodeURI("http://example.com")
'http://example.com'
Pinned Articles
About
ウェブ界隈でエンジニアとして労働活動に励んでいる @gomi_ningen 個人のブログです
Tags
LLM
GenAI
JavaScript
PowerShell
fluentd
kibana
elasticsearch
nginx
イベント
村川梨衣
五十嵐裕美
logrotate
IoT
Java
Scala
ICMP
iputils
C言語
WUG
mastodon
Swift
AWS
Clock
Windows
PoEAA
アーキテクチャ
iOS
DeviceFarm
プログラミング言語
OS
StepFunctions
Lambda
Serverless
terraform
ポエム
RHEL
ネットワーク
GraphQL
CloudWatch
Linux
Coreutils
telnet
network
nc
LinuxKernel
fpinscala
ELB
AppSync
IAM
EFS