jQuery 删除 URL 查询字符串中的加号 (+)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18717557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:18:00  来源:igfitidea点击:

Remove plus sign (+) in URL query string

javascriptjquery

提问by user2764485

I am trying get the string in the following URL to display on my webpage.

我正在尝试获取以下 URL 中的字符串以显示在我的网页上。

http://example.com?ks4day=Friday+September+13th

http://example.com?ks4day=星期五+九月+13日

EDIT:The date in the URL will change from person to person as it's merged in by my CRM program.

编辑:URL 中的日期会因人而异,因为它已被我的 CRM 程序合并。

I can get it to display on my webpage using the code below, the problem is the plus signs (+) come through as well.

我可以使用下面的代码让它显示在我的网页上,问题是加号 (+) 也出现了。

eg. Friday+September+13th

例如。周五+9 月+13 日

What I need it to do is replace the plus signs (+) with spaces so it looks like this:

我需要它做的是用空格替换加号 (+),所以它看起来像这样:

eg. Friday September 13th

例如。9 月 13 日星期五

I'm new to this so I'm having some trouble working it out.

我是新手,所以我在解决这个问题时遇到了一些麻烦。

Any help would be appreciated.

任何帮助,将不胜感激。

This is the code i'm using in a .js file

这是我在 .js 文件中使用的代码

      function qs(search_for) {
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0  && search_for == parms[i].substring(0,pos)) {
            return parms[i].substring(pos+1);;
        }
    }
    return "";
}

This is the code i'm using on my webpage to make it display

这是我在网页上使用的代码以使其显示

     <script type="text/javascript">document.write(qs("ks4day"));</script>

采纳答案by boky

If that's what you are doing, the plus sign will not be the only one that is going to give you a hard time. The apostrophe ('), equals (=), plus (+) and basically anything not in the permitted URL characters (see Percent-encoding@ Wikipedia) is going to get escaped.

如果这就是您正在做的事情,那么加号将不会是唯一让您感到困难的符号。撇号 (')、等于 (=)、加号 (+) 以及基本上任何不在允许的 URL 字符中的内容(请参阅百分比编码@ Wikipedia)都将被转义。

What you are looking for is the decodeURIComponentfunction.

您正在寻找的是decodeURIComponent函数。

回答by Luke

Although Bibhu's answer will work for this one case, you'll need to add decodeURIComponentif you have encoded characters in your URI string. You also want to make sure you do the replace before the decode in case you have a legitimate +in your URI string (as %2B).

尽管 Bibhu 的答案适用于这种情况,但decodeURIComponent如果您的 URI 字符串中有编码字符,则需要添加。您还希望确保在解码之前进行替换,以防万一您+的 URI 字符串中存在合法字符串(如%2B)。

I believe this is the best general way to do it:

我相信这是最好的一般方法:

var x = qs("ks4day");        // 'Friday+September+13th'
x = x.replace(/\+/g, '%20'); // 'Friday%20September%2013th'
x = decodeURIComponent(x);   // 'Friday September 13th'

Here's an example of when it might be useful:

这是一个何时可能有用的示例:

var x = '1+%2B+1+%3D+2'; 
x = x.replace(/\+/g, '%20'); // '1%20%2B%201%20%3D%202'
x = decodeURIComponent(x);   // '1 + 1 = 2'

回答by Bibhu

You can use replace() for this purpose

为此,您可以使用 replace()

var dateString = 'Friday+September+13th';
var s = dateString .replace(/\+/g, ' ');

回答by Pratyush

Parsing strings using regex is often prone to so many errors. Thankfully all modern browsers provide URLSearchParamsto handle params from url strings in a proper way:

使用正则表达式解析字符串通常容易出现很多错误。值得庆幸的是,所有现代浏览器都提供URLSearchParams以适当的方式处理来自 url 字符串的参数:

var params = new URLSearchParams(window.location.search);
var value = params.get('ks4day');
// "Friday September 13th"

Ps: There is also a good polyfillfor old browsers.

Ps:旧浏览器也有很好的polyfill