当前位置:网站首页>What is the difference between escape, encodeuri and encodeuricomponent?
What is the difference between escape, encodeuri and encodeuricomponent?
2022-06-25 14:26:00 【Fat goose 68】
List of articles
One 、 Reference resources
Two 、 Function introduction
2.1 escape(string)
- Concept
escape Is a pair of strings (string) Encoding , So youThis string can be read on all computers. - Return value
Coded string Copy of . Some of these characters have been replaced with hexadecimal escape sequences . - explain
- This method doesn't work for ASCII Code letters and numbers ,
- It's not about the following ASCII Punctuation is encoded : - _ . ! ~ * ’ ( ) .
- All other characters are replaced by escape sequences . (ASCII Some values of the code will also be replaced )
- Inverse function
unescape
const v1 = escape('http://www.w3school.com.cn/')
console.log(v1) // http%3A//www.w3school.com.cn/
const v2 = escape('http://www.w3school.com.cn/My first/')
console.log(v2) // http%3A//www.w3school.com.cn/My%20first/
const v3 = escape(',/?:@&=+$#')
console.log(v3) // %2C/%3F%[email protected]%26%3D+%24%23
console.log(unescape('%2C/%3F%[email protected]%26%3D+%24%23')) // ,/?:@&=+$#
2.2 encodeURI(URIstring)
Definition and Usage
encodeURI() Function can take a string as URI Encoding .Return value
URIstring Copy of , Some of these characters will be replaced by hexadecimal escape sequences .explain
- The purpose of this method is to URI Complete coding , So for the following URI Having a special meaning in ASCII Punctuation ,encodeURI() Functions do not escape :
; / ? : @ & = + $ , # - This method doesn't work for ASCII Code letters and numbers , And it's not about these ASCII Punctuation is encoded :
- _ . ! ~ * ' ( ).
- The purpose of this method is to URI Complete coding , So for the following URI Having a special meaning in ASCII Punctuation ,encodeURI() Functions do not escape :
Inverse function
decodeURI
const v1 = encodeURI('http://www.w3school.com.cn/')
console.log(v1) // http://www.w3school.com.cn/
const v2 = encodeURI('http://www.w3school.com.cn/My first/')
console.log(v2) // http://www.w3school.com.cn/My%20first/
const v3 = encodeURI(',/?:@&=+$#')
console.log(v3) // ,/?:@&=+$#
2.3 encodeURIComponent(URIstring)
Definition and Usage
encodeURIComponent() Function can take a string as URI Components are encoded .Return value
URIstring Copy of , Some of these characters will be replaced by hexadecimal escape sequences .explain
- This method doesn't work for ASCII Code letters and numbers ,
- And it's not about these ASCII Punctuation is encoded :
- _ . ! ~ * ' ( ). - Other characters ( such as :
;/?:@&=+$,#These are used to separate URI Punctuation of components ), Are replaced by one or more hexadecimal escape sequences .
Inverse function
decodeURIComponent
const v1 = encodeURIComponent('http://www.w3school.com.cn/')
console.log(v1) // http%3A%2F%2Fwww.w3school.com.cn%2F
const v2 = encodeURIComponent('http://www.w3school.com.cn/My first/')
console.log(v2) // http%3A%2F%2Fwww.w3school.com.cn%2FMy%20first%2F
const v3 = encodeURIComponent(',/?:@&=+$#')
console.log(v3) // %2C%2F%3F%3A%40%26%3D%2B%24%23
3、 ... and 、encodeURIComponent() function And encodeURI() Difference of function ?
The former assumes that its parameters are URI Part of ( Like agreements 、 Host name 、 Path or query string )
encodeURIComponent() Function uses escape to separate URI Punctuation of parts .
Personal understanding :encodeURIComponent() take URL String to URL The address can identify the slave parameter without affecting the transfer of the parameter , for example (/ : ? & = Everything is standard URL A string that passes parameters , Therefore, it is necessary to escape all ),encode() No effect URL Overall structure , But the parameter is not asscii The value of the code needs to be changed
var set1 = ";,/?:@&=+$"; // Reserved characters
var set2 = "-_.!~*'()"; // Do not escape characters
var set3 = "#"; // Digital signs
var set4 = "ABC abc 123"; // Alphanumeric characters and spaces
console.log(encodeURI(set1)); // ;,/?:@&=+$
console.log(encodeURI(set2)); // -_.!~*'()
console.log(encodeURI(set3)); // #
console.log(encodeURI(set4)); // ABC%20abc%20123 ( The space is encoded as %20)
console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24
console.log(encodeURIComponent(set2)); // -_.!~*'()
console.log(encodeURIComponent(set3)); // %23
console.log(encodeURIComponent(set4)); // ABC%20abc%20123 (the space gets encoded as %20)
Four 、 Use scenarios
If it's just encoding strings , Don't URL It's half a dime , Then use escape.
If you need to code the entire URL, And then you need to use this URL
use encodeURI
encodeURI("http://www.cnblogs.com/season-huang/some other thing");
// "http://www.cnblogs.com/season-huang/some%20other%20thing";
After encoding, it becomes , Spaces are encoded as %20
It was used encodeURIComponent
encodeURI("http://www.cnblogs.com/season-huang/some other thing");
// "http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2Fsome%20other%20thing"
even “/” It's all coded , Whole URL It's useless .
3、 When you need to code URL When the parameter in , that encodeURIComponent It's the best way .
var param = 'http://www.cnblogs.com/season-huang/' // param Is the parameter
param = encodeURIComponent(param) // http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2F
var url = 'http://www.cnblogs.com?next=' + param
console.log(url) // "http://www.cnblogs.com?next=http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2F"
I see a , In the parameter “/” Can code , If you use encodeURI There must be something wrong , Because of the back / It needs to be coded .
边栏推荐
- 算力&NFT交易平台F3.xyz旗下独家权益NFT项目Hash Eagle将盛大发行
- Encapsulating functions and event handling
- Supplementary inheritance and strict mode
- JS component
- What is the safest app for stock account opening? Tell me what you know
- 让PyTorch训练速度更快,你需要掌握这17种方法
- Garbage collection mechanism
- 分享自己平時使用的socket多客戶端通信的代碼技術點和軟件使用
- Laravel8 implementation of picture verification code
- 还没弄明白微服务数据架构事务管理+ACID+一致性+CAP+BASE理论吗,看完彻底解决疑惑
猜你喜欢

shell 数组

"Mobile cloud Cup" computing power network application innovation competition is in hot registration!

BACnet gateway bl103 for building automation

Stream竟然还有应用进阶学习?作为程序员的你知道吗

Problems encountered in debugging botu TCP connection with debugging tool

让PyTorch训练速度更快,你需要掌握这17种方法

权益NFT开创者Hash Eagle如何重新定义NFT,用权益赋能长续价值?

Installation and removal of MySQL under Windows

Does stream even have application advanced learning? As a programmer, you know what

分类器与cross entropy loss函数
随机推荐
turtlebot+lms111+gmapping实践
成员变量与局部变量的区别
"Mobile cloud Cup" computing power network application innovation competition is in hot registration!
Is it safe to open an account with tongdaxin stock?
shell 运算符
Variables, scopes, and variable promotion
Is qiniu regular? Is it safe to open a stock account?
Pourquoi les programmeurs devraient - ils être plus doux?
VGA display of de2-115 FPGA development board
Why should programmers be softer?
【深度学习】多任务学习 多个数据集 数据集漏标
分享自己平時使用的socket多客戶端通信的代碼技術點和軟件使用
腾讯云搭建Socks5多IP代理服务器实现游戏单窗口单IP完美搭建教程附带工具「建议收藏」
To make pytorch faster, you need to master these 17 methods
Which is better and safer, GF easy gold rush or flush
China has made major breakthroughs in battery technology. Japan, South Korea and the United States are lagging behind. China has consolidated its leading edge
API encapsulation of uniapp applet
[untitled]
分类器与cross entropy loss函数
Is it safe for Guosen Securities to open an account?