decodeURI()
decodeURI()
函数对先前由 encodeURI()
或类似例程创建的统一资源标识符 (URI) 进行解码。
¥The decodeURI()
function decodes a Uniform Resource Identifier (URI) previously created by encodeURI()
or a similar routine.
Try it
语法
参数
返回值
例外情况
描述
¥Description
decodeURI()
是全局对象的函数属性。
¥decodeURI()
is a function property of the global object.
decodeURI()
函数通过将 %XX
形式的每个转义序列视为一个 UTF-8 代码单元(一个字节)来解码 URI。在 UTF-8 中,第一个字节中前导 1 位的数量,可以是 0(对于 1 字节 ASCII 字符)、2、3 或 4,表示字符中的字节数。因此,通过读取第一个转义序列,decodeURI()
可以确定还要消耗多少个转义序列。如果 decodeURI()
无法找到预期数量的序列,或者转义序列未编码有效的 UTF-8 字符,则会抛出 URIError
。
¥The decodeURI()
function decodes the URI by treating each escape sequence in the form %XX
as one UTF-8 code unit (one byte). In UTF-8, the number of leading 1 bits in the first byte, which may be 0 (for 1-byte ASCII characters), 2, 3, or 4, indicates the number of bytes in the character. So by reading the first escape sequence, decodeURI()
can determine how many more escape sequences to consume. If decodeURI()
fails to find the expected number of sequences, or if the escape sequences don't encode a valid UTF-8 character, a URIError
is thrown.
decodeURI()
解码所有转义序列,但如果转义序列编码以下字符之一,则转义序列将保留在输出字符串中(因为它们是 URI 语法的一部分):
¥decodeURI()
decodes all escape sequences, but if the escape sequence encodes one of the following characters, the escape sequence is preserved in the output string (because they are part of the URI syntax):
; / ? : @ & = + $ , #
示例
解码西里尔字母 URL
decodeURI() vs. decodeURIComponent()
decodeURI()
假定输入是完整的 URI,因此它不会解码属于 URI 语法一部分的字符。
¥decodeURI()
assumes the input is a full URI, so it does not decode characters that are part of the URI syntax.
decodeURI(
"https://web.nodejs.cn/docs/JavaScript%3A%20a_scripting_language",
);
// "https://web.nodejs.cn/docs/JavaScript%3A a_scripting_language"
decodeURIComponent(
"https://web.nodejs.cn/docs/JavaScript%3A%20a_scripting_language",
);
// "https://web.nodejs.cn/docs/JavaScript: a_scripting_language"
捕获错误
规范
Specification |
---|
ECMAScript Language Specification # sec-decodeuri-encodeduri |
浏览器兼容性
BCD tables only load in the browser