当前位置:网站首页>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 .
边栏推荐
- 第一次读 “Clean” 系列,并没有觉得这是一本多好的书
- Mysql database compressed backup_ Summary of MySQL backup compression and database recovery methods
- Two methods to rollback the code in pycharm to the specified version (with screenshot)
- Clinical Chemistry | 张建中/徐健开发幽门螺杆菌单细胞精准诊疗技术
- golang项目依赖管理工具go vendor,go mod
- Dialogue: recommended system quick start route and summary of knowledge points
- Explain the possible memory leaks caused by the handler at one time and the solutions | the developer said · dtalk
- 通达信股票账户开户安全吗
- Shell built-in commands
- Page 112 machine learning - review of fundamentals of mathematics pptx
猜你喜欢

中国电池技术取得重大突破,日韩美都落后了,中国巩固了领先优势

Shell built-in commands

Report on Hezhou air32f103cbt6 development board

As a software testing engineer, how do you think to ensure software quality?

Variables, scopes, and variable promotion

JVM uses tools to analyze classic cases of OOM

shell 运算符

Logistic Regression VS Linear Regression

Solving error: creating window glfw error: glew initialization error: missing GL version

分享自己平时使用的socket多客户端通信的代码技术点和软件使用
随机推荐
sigmoid函数sigmoid求导
Is qiniu regular? Is it safe to open a stock account?
Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world
Tencent cloud builds a Socks5 multi IP proxy server to realize the perfect building of a game with a single window and a single IP. Tutorial attached tool "suggestions collection"
As a software testing engineer, how do you think to ensure software quality?
Solving error: creating window glfw error: glew initialization error: missing GL version
Variables, scopes, and variable promotion
Complete and detailed compilation of experimental reports
测一测你的挣钱能力有多强?未来的你将从事什么职业?
英語中的九大詞性與九大時態
H5 page graying source code, ie compatible (elegant downgrade provides download browser link)
哈希錶、哈希沖突
JVM uses tools to analyze classic cases of OOM
Two methods to rollback the code in pycharm to the specified version (with screenshot)
How to view the Chrome browser plug-in location
To make pytorch faster, you need to master these 17 methods
turtlebot+lms111+gmapping实践
oracle数据库常用的函数总结
k线图24种经典图解(影线篇)
Basic usage of markdown (plain text and grammar)