Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何以嵌套大括号{}的形式解析具有重复名称的数据?_Javascript_Regex - Fatal编程技术网

Javascript 如何以嵌套大括号{}的形式解析具有重复名称的数据?

Javascript 如何以嵌套大括号{}的形式解析具有重复名称的数据?,javascript,regex,Javascript,Regex,我正在尝试解析以下字符串 EOB { PROCEDURE { /* #1 */ PROCEDURE_LINE="1" ELIGIBLE="002750" DEDUCTIBLE="00000" } PROCEDURE { /* #2 */ PROCEDURE_LINE="2" ELIGIBLE="008725" DEDUCTIBLE="00000" } PROCEDURE { /* #3 *

我正在尝试解析以下字符串

EOB {
   PROCEDURE { /* #1  */
      PROCEDURE_LINE="1"
      ELIGIBLE="002750"
      DEDUCTIBLE="00000"
   }
   PROCEDURE { /* #2  */
      PROCEDURE_LINE="2"
      ELIGIBLE="008725"
      DEDUCTIBLE="00000"
   }
   PROCEDURE { /* #3  */
      PROCEDURE_LINE="3"
      ELIGIBLE="010760"
      DEDUCTIBLE="00000"
   }
   PROCEDURE { /* #4  */
      PROCEDURE_LINE="4"
      ELIGIBLE="021720"
   }
   EMBEDDED_TRANSACTION {
      PROCEDURE { /* #1  */
         PROCEDURE_LINE="1"
         ELIGIBLE="002750"
         DEDUCTIBLE="00000"
      }
      PROCEDURE { /* #2  */
         PROCEDURE_LINE="2"
         ELIGIBLE="008725"
         DEDUCTIBLE="00000"
      }
      PROCEDURE { /* #3  */
         PROCEDURE_LINE="3"
         ELIGIBLE="010760"
         DEDUCTIBLE="00000"
      }
      PROCEDURE { /* #4  */
         PROCEDURE_LINE="4"
         ELIGIBLE="021720"
         DEDUCTIBLE="00000"
      }
   }
}
所需输出(Javascript对象)

这是我试过的

let data=`EOB{
程序{/*#1*/
程序_LINE=“1”
合格=“002750”
免赔额=“00000”
}
程序{/*#2*/
程序_LINE=“2”
合格=“008725”
免赔额=“00000”
}
程序{/*#3*/
程序_LINE=“3”
合格=“010760”
免赔额=“00000”
}
程序{/*#4*/
程序_LINE=“4”
合格=“021720”
}
嵌入式事务{
程序{/*#1*/
程序_LINE=“1”
合格=“002750”
免赔额=“00000”
}
程序{/*#2*/
程序_LINE=“2”
合格=“008725”
免赔额=“00000”
}
程序{/*#3*/
程序_LINE=“3”
合格=“010760”
免赔额=“00000”
}
程序{/*#4*/
程序_LINE=“4”
合格=“021720”
免赔额=“00000”
}
}
}`
让输出=数据。替换(/\/\*。+/g,“”)
.replace(/(.*)(\{)/g,[$1]\n')
.replace(/'*.}/,'')
.replace(/}/g'')
.替换(/^\s*\n/gm,“”)
.replace(/.*(?==)/g,s=>s.toLowerCase())
.替换(/\s+“/g,”);

log(输出)
您必须设计自己的解析器。它类似于部分JSON,但具有XML方面(重复的名称)。有点像JSONXML-ISH无论如何,我接受了挑战,想出了一些让你开始的东西(因为我只看你发布的数据)。您可以根据自己的需要对此进行更改:

function parse(data) {
  var parts = data.match(/\/\*.*?\*\/|{|}|=|".*?"|[a-zA-Z_]+/g); // get only the data parts we want
  var root = {}, o = root, stack = [], name, prop;
  function readValue(s) { return +s || (s[0]=='"' ? s.substring(1, s.length-1) : s); } // read as number, string, or other
  parts.forEach((v, i) => {
     if (!v) return; // skip whitespace
     else if (v.substr(0,2) == '/*') return; // skip comment blocks
     else if (v == '{' && name) { 
        stack.push(o); // get ready to move up a level
        // ... first check of there is already a property here and convert it to an array if so ...
        if (!(name in o)) 
           o = o[name] = {}; // first time we assume no array
        else { // (else there are duplicates with the same name; add as an array instead)
           if (!o[name].length) o[name] = [o[name]]; // if not already an array convert it into one first
           o[name].push(o = {}); // add new object (o is the current object being updated)
        }
        name = ''; // reset
     }
     else if (v == '}') o = stack.pop(); // pop prevfious object from stack
     else if (v == '=') prop = true; // get ready to read a propery value next!
     else if (prop && name) { o[name] = readValue(v); name = ''; prop = false; } // have name and in prop mode, set value and reset
     else name = v; // this can only be a property name, as all other conditions were handled
  });
  return root;
}
用法:
parse其中
s
当然是您的字符串


请注意,这仅在名称重复时创建数组。您可以轻松地将其更改为始终在每个嵌套级别创建一个数组。

这与JSON有关吗?如果是,请相应地标记并使用JSON解析器而不是正则表达式。
function parse(data) {
  var parts = data.match(/\/\*.*?\*\/|{|}|=|".*?"|[a-zA-Z_]+/g); // get only the data parts we want
  var root = {}, o = root, stack = [], name, prop;
  function readValue(s) { return +s || (s[0]=='"' ? s.substring(1, s.length-1) : s); } // read as number, string, or other
  parts.forEach((v, i) => {
     if (!v) return; // skip whitespace
     else if (v.substr(0,2) == '/*') return; // skip comment blocks
     else if (v == '{' && name) { 
        stack.push(o); // get ready to move up a level
        // ... first check of there is already a property here and convert it to an array if so ...
        if (!(name in o)) 
           o = o[name] = {}; // first time we assume no array
        else { // (else there are duplicates with the same name; add as an array instead)
           if (!o[name].length) o[name] = [o[name]]; // if not already an array convert it into one first
           o[name].push(o = {}); // add new object (o is the current object being updated)
        }
        name = ''; // reset
     }
     else if (v == '}') o = stack.pop(); // pop prevfious object from stack
     else if (v == '=') prop = true; // get ready to read a propery value next!
     else if (prop && name) { o[name] = readValue(v); name = ''; prop = false; } // have name and in prop mode, set value and reset
     else name = v; // this can only be a property name, as all other conditions were handled
  });
  return root;
}