encodeURI和encodeURIComponent的区别


本文作者: jsweibo

本文链接: https://jsweibo.github.io/2019/06/25/encodeURI%E5%92%8CencodeURIComponent%E7%9A%84%E5%8C%BA%E5%88%AB/

摘要

本文主要讲述了:

  1. 作用
  2. 区别
  3. 示例
  4. 实现

正文

作用

encodeURI()encodeURIComponent()用于将 UTF-16 字符串的 UTF-8 编码转换为 UTF-8 的转义序列。

区别

encodeURI()encodeURIComponent()的区别在于作用域。

encodeURI()encodeURIComponent()将编码除了下列字符之外的所有字符:

encodeURI() encodeURIComponent()
A-Z A-Z
a-z a-z
0-9 0-9
- -
_ _
. .
! !
~ ~
* *
' '
( (
) )
;
,
/
?
:
@
&
=
+
$
#

示例

英文

&的十六进制 UTF-8 编码为26

1
2
console.log(encodeURI('&')); //"&"
console.log(encodeURIComponent('&')); //"%26"

中文

的十六进制 UTF-8 编码为E4 B8 AD

1
2
console.log(encodeURI('中')); //"%E4%B8%AD"
console.log(encodeURIComponent('中')); //"%E4%B8%AD"

emoji

😊的十六进制 UTF-8 编码为F0 9F 98 8A

1
2
console.log(encodeURI('😊')); //"%F0%9F%98%8A"
console.log(encodeURIComponent('😊')); //"%F0%9F%98%8A"

实现

encodeURI()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
function encodeURIImplementation(str) {
// console.log(str);
return new Promise((resolve, reject) => {
new Blob([str], {
type: 'text/plain',
})
.arrayBuffer()
.then((value) => {
resolve(
Array.from(new Uint8Array(value), function (value) {
// console.log(value);
if (
!/[a-zA-Z0-9\-\_\.\!\~\*\'\(\)\;\,\/\?\:\@\&\=\+\$\#]/.test(
String.fromCodePoint(value)
)
) {
return `%${value.toString(16).toUpperCase()}`;
} else {
return String.fromCodePoint(value);
}
}).join('')
);
});
});
}

const str = 'abcABC😊123中文';
encodeURIImplementation(str).then((value) => {
console.log(value);
console.log(encodeURI(str));
// true
console.log(encodeURI(str) === value);
});
</script>
</body>
</html>

encodeURIComponent()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
function encodeURIComponentImplementation(str) {
// console.log(str);
return new Promise((resolve, reject) => {
new Blob([str], {
type: 'text/plain',
})
.arrayBuffer()
.then((value) => {
resolve(
Array.from(new Uint8Array(value), function (value) {
// console.log(value);
if (
!/[a-zA-Z0-9\-\_\.\!\~\*\'\(\)]/.test(
String.fromCodePoint(value)
)
) {
return `%${value.toString(16).toUpperCase()}`;
} else {
return String.fromCodePoint(value);
}
}).join('')
);
});
});
}

const str = 'abcABC😊123中文';
encodeURIComponentImplementation(str).then((value) => {
console.log(value);
console.log(encodeURIComponent(str));
// true
console.log(encodeURIComponent(str) === value);
});
</script>
</body>
</html>

参考资料

本文作者: jsweibo

本文链接: https://jsweibo.github.io/2019/06/25/encodeURI%E5%92%8CencodeURIComponent%E7%9A%84%E5%8C%BA%E5%88%AB/


本文对你有帮助?请支持我


支付宝
微信