When it comes to web front-end development, there are times when you encounter characters that are represented in the \uxxxx format, where xxxx is the string representation of a 16-digit number, which is called the Unicode escape character in JS and \ r is the same as the escape character. Similar in other languages, there may be other variants of the format.
Most of the time to encounter the need to decode more points, so will introduce the decoding decode, after the introduction of the encoding encode.
The following provides an implementation and a simple description of the different methods in JavaScript C # java three languages, involving typical usage of regular and bitwise operations. the implementation of JavaScript The realization of decoding
1
2
3
|
function decode (s) {return
unescape (S.replace (/\\ (u[0-9a-fa-f]{4})/gm, '%$1 ');
}
|
Unescape is a string that is used to process%uxxxx in such a format, replacing \uxxxx with%uxxxx unescape can be processed. implementation of Coding
1
2
3
4
5
|
function Encode1 (s) {return
escape (s). Replace (/% (u[0-9a-f]{4}) | ( %[0-9A-F]{2})/gm, function ($, $) {return
$ && ' \ + $1.tolowercase () | | unescape ($);}
|
And decoding, use escape encoding, and then replace%uxxxx with \uxxxx, because escape may also encode some characters into%xx format, so these characters also need to use unescape restore back.
Escape encoding Results The XXXX in%UXXXX is uppercase, so subsequent replace only handles uppercase a-f. an implementation of another encoding
Do not use regular and escape