[英]In JavaScript or jQuery, how do you convert a single string with three words (“abc def hij”) into the url encoded format: “abc+def+hij”?
I need to encode some strings to add to a URL. 我需要编码一些字符串来添加到URL。
My string contains multiple words: "abc def hij"
(ie not three separate strings but a single string with 3 words in it ). 我的字符串包含多个单词:
"abc def hij"
(即不是三个单独的字符串,而是包含3个单词的单个字符串 )。
In JavaScript or jQuery, how do you convert a string like that ( "abc def hij"
) into the encoded format: "abc+def+hij"
? 在JavaScript或jQuery中,如何将类似字符串(
"abc def hij"
)转换为编码格式: "abc+def+hij"
?
encodeURIComponent converts the string into percent encoding. encodeURIComponent将字符串转换为百分比编码。
Additionally, 另外,
For application/x-www-form-urlencoded (POST), per http://www.w3.org/TR/html401/interac...m-content-type , spaces are to be replaced by '+', so one may wish to follow a encodeURIComponent replacement with an additional replacement of "%20" with "+".
对于application / x-www-form-urlencoded(POST),根据http://www.w3.org/TR/html401/interac...m-content-type ,空格将替换为'+',所以可能希望跟随encodeURIComponent替换,并使用“+”替换“%20”。
so a simple 这么简单
string.replace(/%20/g, "+");
should do. 应该做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.