Unicode Escape (\uxxxx) encoding and decoding __ encodings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.