我需要编码一些字符串以添加到URL。
我的字符串包含多个字词:"abc def hij"
(即不是三个单独的字符串,而是包含3个字的单个字符串)。
在JavaScript或jQuery中,如何将字符串("abc def hij"
)转换为编码格式:"abc+def+hij"
?
答案 0 :(得分:10)
encodeURIComponent将字符串转换为百分比编码。
此外,
对于application / x-www-form-urlencoded(POST),按http://www.w3.org/TR/html401/interac...m-content-type,空格将替换为'+',因此可能希望跟随encodeURIComponent替换,并附加替换“% 20“与”+“。
这么简单
string.replace(/%20/g, "+");
应该这样做。