escape()

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

注意:escape() 是浏览器实现的非标准功能,只是为了跨引擎兼容性而标准化。它不需要由所有 JavaScript 引擎实现,并且可能无法在所有地方工作。如果可能,请使用 encodeURIComponent()encodeURI()

¥Note: escape() is a non-standard function implemented by browsers and was only standardized for cross-engine compatibility. It is not required to be implemented by all JavaScript engines and may not work everywhere. Use encodeURIComponent() or encodeURI() if possible.

escape() 函数计算一个新字符串,其中某些字符已被十六进制转义序列替换。

¥The escape() function computes a new string in which certain characters have been replaced by hexadecimal escape sequences.

语法

¥Syntax

js
escape(str)

参数

¥Parameters

str

要编码的字符串。

返回值

¥Return value

某些字符已被转义的新字符串。

¥A new string in which certain characters have been escaped.

描述

¥Description

escape() 是全局对象的函数属性。

¥escape() is a function property of the global object.

escape() 函数用转义序列替换所有字符,但 ASCII 单词字符(A–Z、a–z、0–9、_)和 @\*_+-./ 除外。字符由 UTF-16 代码单元转义。如果代码单元的值小于 256,则用两位十六进制数表示,格式为 %XX,必要时左补 0。否则,它由 %uXXXX 格式的四位十六进制数表示,如有必要,在左侧填充 0。

¥The escape() function replaces all characters with escape sequences, with the exception of ASCII word characters (A–Z, a–z, 0–9, _) and @\*_+-./. Characters are escaped by UTF-16 code units. If the code unit's value is less than 256, it is represented by a two-digit hexadecimal number in the format %XX, left-padded with 0 if necessary. Otherwise, it is represented by a four-digit hexadecimal number in the format %uXXXX, left-padded with 0 if necessary.

注意:该函数主要用于 URL 编码,部分基于 RFC 1738 中的转义格式。转义格式不是字符串文字中的 转义序列。你可以将 %XX 替换为 \xXX,将 %uXXXX 替换为 \uXXXX,以获取包含实际字符串文字转义序列的字符串。

¥Note: This function was used mostly for URL encoding and is partly based on the escape format in RFC 1738. The escape format is not an escape sequence in string literals. You can replace %XX with \xXX and %uXXXX with \uXXXX to get a string containing actual string-literal escape sequences.

示例

¥Examples

使用 escape()

¥Using escape()

js
escape("abc123"); // "abc123"
escape("äöü"); // "%E4%F6%FC"
escape("ć"); // "%u0107"

// special characters
escape("@*_+-./"); // "@*_+-./"

规范

Specification
ECMAScript Language Specification
# sec-escape-string

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看