我需要对一些字符串进行编码以添加到 URL。
我的字符串包含多个单词:"abc def hij"
(即不是三个单独的字符串,而是一个包含 3 个单词的字符串)。
在 JavaScript 或 jQuery 中,如何将这样的字符串 ("abc def hij"
) 转换为编码格式:"abc+def+hij"
?
最佳答案
encodeURIComponent将字符串转换为百分比编码。
此外,
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 "+".
很简单
string.replace(/%20/g, "+");
应该做。
关于javascript - 在 JavaScript 或 jQuery 中,如何将包含三个单词的单个字符串 ("abc def hij") 转换为 url 编码格式 : "abc+def+hij"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4105556/