JavaScript 字符串编码解码

escape & unescape
encodeURI & decodeURI
encodeURIComponent & decodeURIComponent

escape & unescape

除字母和数字,以及*@-_+./之外的字符进行转义

1
2
3
4
5
console.log(escape("https://blog.irember.com/2018-03/JavaScript-字符串编码解码/"));
console.log(unescape("http%3A//blog.irember.com/2018-03/JavaScript-%u5B57%u7B26%u4E32%u7F16%u7801%u89E3%u7801/"));

//http%3A//blog.irember.com/2018-03/JavaScript-%u5B57%u7B26%u4E32%u7F16%u7801%u89E3%u7801/
//https://blog.irember.com/2018-03/JavaScript-字符串编码解码/

encodeURI & decodeURI

除字母和数字,以及-_.!~*’()之外的字符进行转义

1
2
3
4
5
console.log(encodeURI("https://blog.irember.com/2018-03/JavaScript-字符串编码解码/"));
console.log(decodeURI("https://blog.irember.com/2018-03/JavaScript-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%BC%96%E7%A0%81%E8%A7%A3%E7%A0%81/"));

//https://blog.irember.com/2018-03/JavaScript-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%BC%96%E7%A0%81%E8%A7%A3%E7%A0%81/
//https://blog.irember.com/2018-03/JavaScript-字符串编码解码/

encodeURIComponent & decodeURIComponent

除字母和数字,以及-_.!~*’()之外的字符进行转义

1
2
3
4
5
console.log(encodeURIComponent("https://blog.irember.com/2018-03/JavaScript-字符串编码解码/"));
console.log(decodeURIComponent("http%3A%2F%2Fblog.irember.com%2F2018-03%2FJavaScript-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%BC%96%E7%A0%81%E8%A7%A3%E7%A0%81%2F"));

//http%3A%2F%2Fblog.irember.com%2F2018-03%2FJavaScript-%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%BC%96%E7%A0%81%E8%A7%A3%E7%A0%81%2F
//https://blog.irember.com/2018-03/JavaScript-字符串编码解码/
0%