简体   繁体   English

用JavaScript RegEx替换嵌套的双引号

[英]Replace nested double quotes with JavaScript RegEx

I have a JSON-stringified object as follows. 我有一个JSON字符串化的对象,如下所示。 I need to parse the object using JSON.parse . 我需要使用JSON.parse解析对象。 I am facing nested double quotes which I want to replace with RegEx. 我面临的嵌套双引号我想用RegEx替换。

// input string 
var stringifyObj = '{
"a": "abcd"",
  "b": false,
  "c": [
    {
      "c": "1234",
      "d": "abcd",
      "e": [
        {
          "cs": "cs",
          "df": true
        }
      ],
      "g": "USE",
      "f": false,
      "g": "QA DCG",
      "h": "333 Market St",
      "i": "San Francisco",
      "j": " 94568"
    },
    {
      "k": "20570",
      "l": "test",
      "m": [
        {
          "n": "USD",
          "o": true
        }
      ],
      "p": "US",
      "q": true,
      "r": "Wires_defect 13205 testing",
      "s": "333 market street_retesting 13205",
      "t": "SF_test",
      "u": "ABCD"",
      "v": false
    }
  ]
}';

How can I parse this stringify object "a and u" properties contains nested double quotes. 如何解析此字符串化对象的“ a和u”属性包含嵌套的双引号。

I tried to replace but did not work : replace(/(".*?"):(".*?)"(.*?")/g,"$1:$2\\\\\\"$3") 我尝试替换,但是没有用: replace(/(".*?"):(".*?)"(.*?")/g,"$1:$2\\\\\\"$3")

depends on the values expected in the String, how many quotes, special char, and so on ... 取决于字符串中期望的值,多少个引号,特殊字符等等。

but for this special case you could use this, or at least use it as starting point 但是对于这种特殊情况,您可以使用它,或者至少使用它作为起点

 var bad_json_string = '{"a":"abc"@#"}'; var cleaned_string = bad_json_string.replace(/([^:]+:")(.*)"(.*"})/g, function(){return RegExp.$1 + RegExp.$2 +"'"+ RegExp.$3 }); document.write(cleaned_string); // tested on Win7 with chrome 44+ 

Update 1: 更新1:

The updated json is not easy fixed with javascript RegExp Expression. 使用javascript RegExp Expression不容易解决更新的json。 With nested List/Objects/... 与嵌套的列表/对象/ ...

I would suggest cleaning up the Json from the server, if that is not possible than I would write a function to "parse" the string in an other manner. 我建议从服务器清理Json,如果那不可能的话,我会写一个函数以其他方式“解析”字符串。

Update 2: 更新2:

A possible Function-Solution could/should be this( just quick demo, review/ optimize for production usage ) 一个可能的功能解决方案可能是/应该是这样( 只是快速演示,查看/ 优化生产用途

 var bad_json_string = '{\\r\\n"a": "abcd"",\\r\\n "b": false,\\r\\n "c": [\\r\\n {\\r\\n "c": "1234",\\r\\n "d": "abcd",\\r\\n "e": [\\r\\n {\\r\\n "cs": "cs",\\r\\n "df": true\\r\\n }\\r\\n ],\\r\\n "g": "USE",\\r\\n "f": false,\\r\\n "g": "QA DCG",\\r\\n "h": "333 Market St",\\r\\n "i": "San Francisco",\\r\\n "j": " 94568"\\r\\n },\\r\\n {\\r\\n "k": "20570",\\r\\n "l": "test",\\r\\n "m": [\\r\\n {\\r\\n "n": "USD",\\r\\n "o": true\\r\\n }\\r\\n ],\\r\\n "p": "US",\\r\\n "q": true,\\r\\n "r": "Wires_defect 13205 testing",\\r\\n "s": "333 market street_retesting 13205",\\r\\n "t": "SF_test",\\r\\n "u": "ABCD"",\\r\\n "v": false\\r\\n }\\r\\n ]\\r\\n}'; var t = bad_json_string.match(/^(.| )*$/gm); function unQuote(string){ return string.replace(/^"/,"").replace(/"$/,""); } function trim(string, chr){ return string.replace(new RegExp("^(" + (chr || " " )+")*"),"").replace(new RegExp( "(" + (chr || " ") + ")*$"),""); } var clean = ""; function buildObj(list, idx){ if(list.length<=idx) return; var item = trim(list[idx]); switch(item){ case "{": case "[": case "}": case "]": case "},": case "],": clean += item; break; case "": break; default: var line = item.split(":"); var index = unQuote(line[0]); var value = trim(trim(line[1]),","); clean += index + ":"; if(value.indexOf("\\"")==0){ clean += "'"+ unQuote(value) + "',"; } else if(value.indexOf("{")==0 || value.indexOf("[")==0){ clean += value; } else{ clean += value + ","; } } buildObj(list, ++idx); } buildObj( t, 0); document.write(clean); // to get the object you could do this, but dont forget eval==evil. :) var t; eval("t="+clean); document.write("<br/><br/><br/>"); document.write("Object prop 'a' => " + t['a']); // tested on Win7 with Chrome 44+ 

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

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