📜  JavaScript 中 unescape() 和 escape() 函数的区别

📅  最后修改于: 2022-05-13 01:56:54.635000             🧑  作者: Mango

JavaScript 中 unescape() 和 escape() 函数的区别

在本文中,我们将了解 JavaScript 中的 escape() 和 unescape() 函数。我们将通过示例了解使用这两个功能的目的。在本文后面,我们将讨论 escape() 和 unescape() 函数之间的区别。让我们讨论一下 escape()函数。

1. escape()函数:该函数将字符串作为单个参数,对可以在支持ASCII字符的计算机网络上传输的字符串进行编码。编码是将纯文本转换为密文的过程。

句法:

escape( string )

参数:此函数接受单个参数:

  • 字符串:此参数保存将被编码的字符串。

返回值:返回一个编码的字符串。

注意: escape()函数只对特殊字符进行编码,此函数已弃用。

例外: @ - + 。 / * _

示例:在此示例中,我们使用特殊字符来查看更改。

Javascript


Javascript


Javascript


输出:

Geeks%20for%20Geeks%21%21%21
To%20contribute%20articles%20contact%20us%20atcontribute
@geeksforgeeks.org 

从上面的输出中,我们可以看到带有特殊符号“@”的电子邮件地址中的异常未编码,显示与输入相同,其余文本已编码。

现在,如果我们想将编码文本(即密文)转换为普通可读文本,那么我们必须使用unescape()函数来解码编码文本。解码是将密文转换为纯文本的过程。

2. unescape()函数:该函数将字符串作为单个参数,并使用它来解码由 escape()函数编码的字符串。字符串中的十六进制序列在通过 unescape()函数解码时被它们所代表的字符替换。

句法:

unescape(string)

参数:此函数接受单个参数:

  • 字符串:此参数保存将被解码的字符串。

返回值:返回解码后的字符串。

注意:此函数仅解码特殊字符,此函数已弃用。

例外: @ - + 。 / * _

示例 1:在此示例中,我们使用特殊字符来查看更改。

Javascript


                     

输出:

Geeks for Geeks!!!
To contribute articles contact us at  
contribute@geeksforgeeks.org

从上面的例子中,我们可以看到使用 unescape()函数将密文解码为纯文本。

示例 2:

Javascript


输出:

Encoded : Geeks%20for%20Geeks%21%21%21
Decoded : Geeks for Geeks!!!

Encoded : To%20contribute%20articles%20contact%20us%20
          at%20contribute@geeksforgeeks.org
Decoded : To contribute articles contact us at 
          contribute@geeksforgeeks.org

unescape() 和 escape()函数的区别:

 

unescape()

escape()

1.

The unescape() function is used to decode that string encoded by the escape() function.

The escape() function in JavaScript is used for encoding a string. Using ASCII character support, it makes a string portable so that it can be transmitted across any network to any computer.

2.

A decoded string is returned.

An encoded string is returned.

3.

This function only encodes the special characters, this function is deprecated.

It has certain Exceptions: @ – + . / * _

This function only encodes the special characters, this function is deprecated.

It has certain Exceptions: @ – + . / * _