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

标签 javascript regex

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

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 对象)

{
    EOB: {
        PROCEDURE: [
            {
                PROCEDURE_LINE="1",
                ELIGIBLE="002750",
                DEDUCTIBLE="00000"
            } ... (other procedures)
        ],
        EMBEDDED_TRANSACTION: [
            {
                PROCEDURE_LINE="1",
                ELIGIBLE="002750",
                DEDUCTIBLE="00000"
            } ,,, (other procedures)
        ]
    }
}

这是我尝试过的

let data = `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"
  }
   }
}`

  let output =  data.replace(/\/\*.+/g, '')
                    .replace(/(.*)(\{)/g, '[$1]\n')
                    .replace(/'*.}/, '')
                    .replace(/}/g, '')
                    .replace(/^\s*\n/gm, "")
                    .replace(/.*?(?==)/g, s => s.toLowerCase())
                    .replace(/\s+"/g, '"');

console.log(output)

最佳答案

您必须设计自己的解析器。它就像部分 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); 其中s 当然是您的字符串。 ;)

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

关于javascript - 如何解析具有重复名称的嵌套大括号 { } 形式的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58072490/

相关文章:

regex - Pig - 使用正则表达式提取字符串

javascript - 将一个字符串一分为二,保持单词完整并返回它们

node.js - 在 TypeScript(也是普通的 Nodejs)中使用正则表达式解析文件名,令人惊讶的是结果为 null

用于存储和使用数据的 Javascript 函数(类型)

javascript - 在 jQuery 和 Javascript 之间使用 JSON 数据

Java RegEx 替换 XML 标签名称

regex - Vim — 在不以 ‘foo’ 开头的行上用 ‘bar’ 替换 ‘character’

javascript - 当鼠标离开元素时,CSS 过渡过早结束

javascript - Express body-parser : req. body 返回空对象

javascript - 通过单击链接更改 iframe src