Javascript escape() function and unescape() function

Javascript escape() function and unescape() function

Table of contents

Javascript escape() function and unescape() function

1. escape function

2. unescape() function


 

1. escape function

The main function of the escape() function is to encode strings so that they are readable on all computers.

grammar:

escape(charString)

illustrate:

charString is a required parameter and represents the string or text to be encoded. The escape() function returns a string value (Unicode format) containing the contents of charString. Except for a few symbols such as "*@", all spaces, punctuation marks, and other non-ASCII characters can be replaced by encoding in the form of "%xx", where xx is equal to the hexadecimal number representing the character.

Example:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        document.write(escape("hello lvye!"))
    </script>
</head>
<body>
</body>
</html>

The preview effect in the browser is as follows:

 

e9e8231377cf251472a7d4aa05167939.png

analyze:

The encoding corresponding to the space character is "%20", and the encoding corresponding to the exclamation mark is "%21", so the result after executing escape("hello lvye!") is "hello%20lvye%21".

 

2. unescape() function

The escape() function and the unescape() function are just the opposite. The former is encoding and the latter is decoding.

grammar:

unescape(charString)

illustrate:

charString is a required parameter and represents the string to be decoded. The unescape() function returns an ASCII string of the specified value. Contrary to the escape() function, the unescape() function returns a string value containing the contents of charString. All characters encoded in the "%xx" hexadecimal form are replaced by equivalent characters in the ASCII character set.

Example:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        document.write(unescape("hello%20lvye%21"))
    </script>
</head>
<body>
</body>
</html>

The preview effect in the browser is as follows:

 

94b6a131da9b542b3e60446ed12413e8.png

analyze:

The encoding corresponding to the space character is "%20", and the encoding corresponding to the exclamation mark is "%21", so the result after executing unescape("hello%20lvye%21") is "hello lvye!".

 

Guess you like

Origin blog.csdn.net/2301_78835635/article/details/134738386