如何在 javascript 中用 <comma> 替换 %2C?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9151566/
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-10-26 05:45:03  来源:igfitidea点击:

How can I replace %2C with a <comma> in javascript?

javascript

提问by Ryan

 #&q=car&category=Car%20Audio%2CAccessories&brand=

I borrowed a this function from a previous question asked on SO:

我从上一个关于 SO 的问题中借用了这个函数:

function insertParam(key, value)
{
key = escape(key); value = escape(value);

var kvp = document.location.hash.substr(1).split('&');

var i=kvp.length; var x; while(i--) 
{
    x = kvp[i].split('=');

    if (x[0]==key)
    {
            x[1] = value;
            kvp[i] = x.join('=');
            break;
    }
}

if(i<0) {kvp[kvp.length] = [key,value].join('=');}

//this will reload the page, it's likely better to store this until finished

document.location.hash = kvp.join('&'); 
}

I use it like this:

我像这样使用它:

    insertParam("category",xy);
    insertParam("brand",zy);

My problem is it is decoding comma's to %2C. I know I can handle the characters on the server side, but how can I make it look pretty with javascript? By pretty I mean replace %2c with a comma.

我的问题是它正在将逗号解码为 %2C。我知道我可以处理服务器端的字符,但是我怎样才能用 javascript 让它看起来漂亮呢?漂亮我的意思是用逗号替换 %2c 。

采纳答案by copy

decodeURIComponent(foo)is the thing you are looking for.

decodeURIComponent(foo)是你正在寻找的东西。

Edit: Misread your question.

编辑:误读你的问题。

Use replace(/&/g, "%26").replace(/=/g, "%3D")instead of escapeon key and value to do this.

使用replace(/&/g, "%26").replace(/=/g, "%3D")代替escape键和值来执行此操作。

None of the 3 functions encodeURI, encodeURIComponentor encodework for this task, because they either encode commas or don't encode &=.

无的3个功能encodeURIencodeURIComponentencode工作完成这个任务,因为它们要么编码逗号或不编码&=

回答by Cheery

I do not know why in the previous answer that was striked out, but the answer was correct.

我不知道为什么在以前的答案中被删除了,但答案是正确的。

alert(decodeURIComponent('%2C'));

So, you break your query strings into elements, splitting by & symbol. Than you split the results by = symbol and apply decodeURIComponent on both name and the value.

因此,您将查询字符串分解为元素,并按 & 符号拆分。比你用 = 符号分割结果并对名称和值应用 decodeURIComponent 。

ps: key = escape(key); value = escape(value);you should not use escape here (it is different for different browsers. and by 'different' I meant IE). Use encodeURIComponent.

ps:key = escape(key); value = escape(value);你不应该在这里使用转义(不同的浏览器是不同的。“不同”我的意思是IE)。使用 encodeURIComponent。

pps: because they either encode commas or don't encode &=???

pps:because they either encode commas or don't encode &=???

alert(encodeURIComponent('&=,'));

outputs %26%3D%2C

输出 %26%3D%2C

回答by hobs

This worked for me for undoing encodeURIComponent()on URIs that contain commas:

这对我来说适用于撤消encodeURIComponent()包含逗号的 URI:

.replace(/%2C/g,",")