javascript 百分比编码javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4911820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Percent encoding javascript
提问by locoboy
Is there a javascript function that takes a string and converts it into another string that is percent-encoded? That way something like "This Guy" turns into "This%20Guy".
是否有一个 javascript 函数接受一个字符串并将其转换为另一个百分比编码的字符串?这样像“This Guy”这样的东西就会变成“This%20Guy”。
Thanks
谢谢
回答by Dr.Molle
Try encodeURIComponent()or escape()
尝试encodeURIComponent()或escape()
回答by kirilloid
encodeURI, encodeURIComponentor escapewill work the same way for yourstring, but they differ in details.
encodeURI,encodeURIComponent或者escape对您的字符串以相同的方式工作,但它们在细节上有所不同。
encodeURIis just for escaping URLsencodeURIComponentalso escapes =and &escapeworks differently with non-ASCII unicode symbols
encodeURI仅用于转义 URLencodeURIComponent也可以转义,=并且&escape与非 ASCII Unicode 符号的工作方式不同
encodeURI("Ω") === encodeURIComponent("Ω") === "%CE%A9"
escape("Ω") === "%u03A9"
if you need to send a string as part of request, use encodeURIComponent
如果您需要将字符串作为请求的一部分发送,请使用 encodeURIComponent
回答by codemirror
Try this encodeURIComponent()
试试这个 encodeURIComponent()
var stringToDecode = "J&K";
var encodedString = encodeURIComponent(stringToDecode );
Use decodeURIComponent()to decode it again when needed
使用decodeURIComponent()在需要的时候再次进行解码
More Info here
更多信息在这里
https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
回答by Gustavo Costa De Oliveira
Yes, here is
是的,这里是
escape('This Guy');

