Python to restore the string encoded by the JavaScript escape function, pythonescape

Source: Internet
Author: User

Python to restore the string encoded by the JavaScript escape function, pythonescape

If you encounter a problem, you need to use Python to restore the escape in JavaScript. However, after a long time, you have not found the answer, so you have to study the solution in depth.
Let's take a look at the encoding of a text section of escape in js.
Copy codeThe Code is as follows:
A = escape ('this is a string of text ');
Alert ();

Output:
Copy codeThe Code is as follows:
% U8FD9 % u662F % u4E00 % u4E32 % u6587 % u5B57

At first glance, it seems a bit similar to the json format. Let's take a look at the man with the same standard json format encoding "this is a string of text"
Copy codeThe Code is as follows:
# Encoding = UTF-8
Import json
A = 'this is a string of text'
Print json. dumps ()

Output:
Copy codeThe Code is as follows: "\ u8fd9 \ u662f \ u4e00 \ u4e32 \ u6587 \ u5b57"
After comparison, in fact, each man in js escape encoding is a "% u" symbol plus a four-character encoding, while each man in json encoding is a "\ u" symbol plus a four-character encoding, in this case, we can use the string replacement operation to restore the json format, and then use the json module loads.
Copy codeThe Code is as follows:
# Encoding = UTF-8
Import json
 
# Js escape string Encoding
C = '% u8FD9 % u662F % u4E00 % u4E32 % u6587 % u5B57'
 
# Restore a Json object
JsonObj = '"' +" ". join ([(I and" \ "+ I) for I in c. split ('%')]) + '"'
 
Print json. loads (jsonObj)

In particular, after replacing "%" with the "\" symbol, you must use double quotation marks to pack the string to make it a json object and then use json. loads.
Later, I found a simpler method on a website. The Code is as follows:
Copy codeThe Code is as follows:
# Encoding = UTF-8
C = '% u8FD9 % u662F % u4E00 % u4E32 % u6587 % u5B57'
Print "". join ([(len (I)> 0 and unichr (int (I, 16) or "") for I in c. split ('% U')])

In fact, the idea is similar. Replace "% u", and the rest are 4 characters of Fixed Length Encoding. Finally, unichr uncodes back the Chinese characters.


How to translate the characters compiled by the escape Function in JavaScript?

First define a function code as follows
Function aa ();
{
Var a = unescape (string content );
}

Run the following statement to run the function:
Confirm (aa ());

That's all.

Functions of the escape () function in JavaScript

The escape () function can encode a string so that the string can be read on all computers.
Instance
In this example, we will use escape () to encode the string:
<Script type = "text/javascript">

Document. write (escape ("Visit W3School! ") +" <Br/> ")
Document. write (escape ("?! = () # % &"))

</Script>
Output:
Visit % 20W3School % 21
% 3F % 21% 3D % 28% 29% 23% 26
Reference: www.w3school.com.cn/js/jsref_escape.asp
 

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.