简体   繁体   English

在JavaScript或jQuery中,如何将带有三个单词(“abc def hij”)的单个字符串转换为url编码格式:“abc + def + hij”?

[英]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.

相关问题 如何从“ abc def”获取abc? - How to get abc from “abc def”? 如果网址具有使用jQuery的特定术语,例如/ abc-def /,则必须删除特定的Cookie。 - I have to delete the particular Cookie if the url has specific term like /abc-def/ using jQuery. 如何在之后插入Div - How to insert a Div after <Div class=“abc def”style='display“> or before <Div class=”aB cd efG"> in Gmail Page using javascript or Jquery? 匹配[ABC DEF](http://google.com) - Match [ABC DEF](http://google.com) 如何使用 document.getElementById(“abc”).innerHTML DOM 以字符串格式显示 JavaScript object 的内容? - How do I display the content of a JavaScript object in a string format using document.getElementById(“abc”).innerHTML DOM? 使用Javascript我如何搜索字符串中的前三个字母,看看它们是否匹配“ABC”? - With Javascript how would I search the first three letters in a string and see if they match “ABC”? Rails def中的Javascript / Jquery选择器($) - Javascript / Jquery selector ($) in a rails def JavaScript:Object(&#39;abc&#39;)有什么作用? - JavaScript: What does Object('abc') do? 如何删除字符串中除“ abc”以外的所有匹配项? - How to remove all but one occurrences of 'abc' in a string? JavaScript的简单abc排序 - Simple abc sorting for javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM