JS DOM: escape, unescape ❌
The Escape Method
escape is a string property of the
the Global Object.
escape is deprecated.
escape(str)-
Return a new string from str with some characters percent encoded.
console.log(escape("A")); // A console.log(escape("~")); // %7E console.log(escape(" ")); // %20 console.log(escape("α")); // %u03B1 /* α GREEK SMALL LETTER ALPHA ID 945 HEXD 3B1 UTF8 CE B1 UTF16 3B1 */ console.log(escape("🦋")); // %uD83E%uDD8B /* 🦋 Name: BUTTERFLY ID 129419 HEXD 1F98B UTF8 F0 9F A6 8B UTF16 D83E DD8B */
Forms of the Changed Char
Changed characters are in this form:
%DD→ for char with codepoint ≺ 256.%uDDDD→ for char with codepoint ≥ 256.
, where DD is 2 digits of hexadecimal and DDDD is 4 digits of hexadecimal.
they are the byte sequence of the character in UTF-16 encoding. .
〔see JS: String Index Code Unit〕
Chars Not Changed
// all chars that escape() does not change console.log((Array.from((Array(128)).keys(), (x) => String.fromCharCode(x)).filter((x) => (escape(x) === x))).join("")); // *+-./0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz
All other chars are replaced.
unescape
Use unescape function to decode a string encoded with escape.