JavaScript encodeURIComponentメソッドで文字列をエンコードするサンプル

環境
Google Chrome  107.0.5304.88
Windows 10 Home 64bit

構文
encodeURIComponent(str);
引数
str
URI の構成要素となる文字列です。

返値
与えられた文字列を表す URI 構成要素としてエンコードされた新しい文字列です。

戻り値
encodeURIComponent は下記を除くすべての文字をエスケープします。
エスケープされないもの:
A-Z a-z 0-9 – _ . ! ~ * ' ( )

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const strA = 'テストAB3434';
console.log(encodeURIComponent(strA));
const strB = 's6-?:#$&=_.!~*\'()';
console.log(encodeURIComponent(strB));
const strA = 'テストAB3434'; console.log(encodeURIComponent(strA)); const strB = 's6-?:#$&=_.!~*\'()'; console.log(encodeURIComponent(strB));
const strA = 'テストAB3434';
console.log(encodeURIComponent(strA));

const strB = 's6-?:#$&=_.!~*\'()';
console.log(encodeURIComponent(strB));

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
> "%E3%83%86%E3%82%B9%E3%83%88AB34%EF%BC%93%EF%BC%94"
> "s6-%3F%3A%23%24%26%3D_.!~*'()"
> "%E3%83%86%E3%82%B9%E3%83%88AB34%EF%BC%93%EF%BC%94" > "s6-%3F%3A%23%24%26%3D_.!~*'()"
> "%E3%83%86%E3%82%B9%E3%83%88AB34%EF%BC%93%EF%BC%94"
> "s6-%3F%3A%23%24%26%3D_.!~*'()"

 

JavaScript

Posted by arkgame