简体   繁体   English

JavaScript unescape?

[英]JavaScript unescape?

I am escaping & and = before parsing to an object like this: 我在解析为这样的对象之前转义&和=:

var obb = parseJSON('{"' + text.replace(/&/g, "\",\"").replace(/=/g,"\":\"") + '"}');

When the object is created i get: 创建对象时,我得到:

obb.name
obb.date
obb.text

Inside obb.text i want to revert the escape done above.. 在obb.text内部,我想还原上面完成的转义。

I've tried: 我试过了:

text.replace(/\&/g, "&").replace(/\=/g, "=")

When i run the following on obb.text 当我在obb.text上运行以下内容时

obb.text = decodeURIComponent(escape(obb.text));

I get parse errors 我收到解析错误

But does not seem to work 但是似乎不起作用

Any ideas? 有任何想法吗?

Rather than keeping your fingers crossed that your output data does not contain anything that would cause the JSON parser to barf up, consider this method based on HTTP RFC parameter parsing. 与其让您的输出数据不包含任何会导致JSON解析器阻塞的内容,不如考虑使用基于HTTP RFC参数解析的方法。 The RFC RFC (2616) states the following rules: RFC RFC(2616)规定以下规则:

  • All fields are separated by & 所有字段均以&分隔
  • All field names are before an =, values after =, value is optional 所有字段名称都在=之前,=之后的值,value是可选的
  • [] denotes "one more element to this as an array", []表示“此数组还有一个元素”,

They also tentatively suggest the following rule, which I will offer you a choice on: - A parameter without [] overwrites its previous versions if submitted (this is not followed by all webservers and is the matter of HTTP fragmentation/pollution attacks, by the way) 他们还临时建议以下规则,我将为您提供以下选择:-如果参数不带[],则提交后会覆盖其以前的版本(并非所有Web服务器都遵循该参数,这是HTTP分段/污染攻击的问题)方式)

We're going to parse following this exact structure, assuming that stuff has been properly character-encoded. 我们将按照这个确切的结构进行解析,假设已正确地对字符进行了字符编码。 The start of the code should look like this: 代码的开始应如下所示:

var myObj = {};
var k = myPostbackContent.split("&");
for (var i = 0; i < k.length; i++) {
  var kinfo = k[i].split("=");
  var key = kinfo[0];
  if (kinfo[1] !== undefined) {
     var value = kinfo[1];
  };
  if (key.substr(-2) == "[]") {
     key = key.substr(0,key.length-2);
     if (myObj[key] === undefined) myObj[key] = [];
     if (myObj[key] instanceof Array) myObj[key].push(value);
  }
  else {

The following part is dependent on your assumptions: 以下部分取决于您的假设:

  • If you would like your elements to overwrite each other, put in the else the following version: 如果您希望元素互相覆盖,请在else中添加以下版本:

      myObj[key] = value; 
  • If, instead, you would prefer to have the first instance of an element have precedence, put the following: 相反,如果您希望某个元素的第一个实例具有优先级,请输入以下内容:

      if (myObj[key] === undefined) myObj[key] = value; 
  • If, like IIS, you'd prefer to have the element auto-append to a string separated by , , use the following: 如果,如IIS,你宁愿有元件自动追加到分隔字符串,使用下列内容:

      if (myObj[key].length) myObj[key] += ","; myObj[key] += value; 

I've built a little TinkerIO script to show you how all three works. 我构建了一个小小的TinkerIO脚本,向您展示了这三种方法的工作原理。 If this is not what you were looking for, do let me know. 如果这不是您想要的,请告诉我。 The default behaviour is "overwrite", by the way. 顺便说一句,默认行为是“覆盖”。

This method can be applied in reverse to go from an object to an URI-encoded string, by the way. 顺便说一下,可以反向应用此方法,以从对象转换为URI编码的字符串。 Just loop through your object's properties and go by key=value, joining all the elements with a &. 只需遍历对象的属性并按key = value,将所有元素与&相连即可。

As long as your text parameter is always at the end of the string you could do something like this: 只要您的text参数始终位于字符串的末尾,就可以执行以下操作:

var obb = {};
obb.name = text.match(/name=([^&]+)/)[1];
obb.date = text.match(/date=([^&]+)/)[1];
obb.text = text.match(/text=(.+)/)[1];

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM