javascript encodeURIComponent并将空格转换成+符号
16lz
2021-01-22
I would like to encode my URL, but I want to convert spaces to plus symbols.
我想对URL进行编码,但是我想把空格转换成加号。
This is what I attempted to do...
这就是我想做的……
var search = "Testing this here &";
encodeURIComponent(search.replace(/ /gi,"+"));
The output from that is Testing%2Bthis%2Bhere%2B%26
but what I would like it to be is Testing+this+here+%26
I tried replacing the space with %20
to convert it into a plus symbol, but that didn't seem to work. Can anyone tell me what it is I'm doing wrong here?
这里的输出是测试%2Bthis%2Bhere%2B%26但是我想要的是测试+这里+%26我试着用%20来替换这个空间,把它转换成一个加号,但是这似乎没有效果。有人能告诉我我在这里做错了什么吗?
2 个解决方案
#1
35
encodeURIComponent(search).replace(/%20/g, "+");
What you're doing wrong here is that first you convert spaces to pluses, but then encodeURIComponent
converts pluses to "%2B"
.
你做错的是,首先你把空格转换成加号,然后encodeURIComponent把加号转换成“%2B”。
更多相关文章
- python1.返回一个字符串中出现次数第二多的单词 2.字符串中可能
- 计算所有字符,包括linux中的空格
- postgresql 数组 多了引号 空格处理
- sql2005指定字段插入空格。
- android textview空格占位符以及一些其他占位符汇总
- javascript去掉前后空格
- Java操作ini文件 ,解决properties文件中无法读取换行及空格
- Java将一个字符串中的多个连一起的空格变成单个空格