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”。

更多相关文章

  1. python1.返回一个字符串中出现次数第二多的单词 2.字符串中可能
  2. 计算所有字符,包括linux中的空格
  3. postgresql 数组 多了引号 空格处理
  4. sql2005指定字段插入空格。
  5. android textview空格占位符以及一些其他占位符汇总
  6. javascript去掉前后空格
  7. Java操作ini文件 ,解决properties文件中无法读取换行及空格
  8. Java将一个字符串中的多个连一起的空格变成单个空格

随机推荐

  1. 【起航计划 002】2015 起航计划 Android
  2. 华为android 10 手机恢复删除的图片,目前
  3. 搭建自己的Android浏览器(一)
  4. 在android上运行native可执行程序
  5. Android自定义相机超详细讲解
  6. Android处理屏幕旋转时的解决方案
  7. Android格式化外部设备如USB等
  8. 【知识点】android中的adapter
  9. Android 开发入门
  10. android横屏竖屏处理