简体   繁体   English

如何通过正则表达式仅对 JSON 键着色?

[英]How to color only JSON keys via regex?

In javascript, I want to capture all of my json keys via regex.在 javascript 中,我想通过正则表达式捕获我所有的 json 键。 Let's assume that the json outputs with every key-value pair in a new line.让我们假设 json 输出与新行中的每个键值对。 Eg例如

{
    "foo": "bar",
    "age": 23,
}

In other words, every first instance of a string with double quotes.换句话说,字符串的每个第一个实例都带有双引号。

I don't think regular expressions will cut it here, I would suggest you build an output string based off of your JSON: 我不认为正则表达式会在这里删掉,我建议您根据JSON构建输出字符串:

 let json = { foo: "bar", age: 23, test1: 1, test2: 2, test3: 3, }; let formattedJSONString = Object.entries(json) .reduce((acc, [key, value]) => `${acc} <span class='json-key'>"${key}": </span> <span class='value'>"${value}"</span>,<br/>`, `{<br/>`) + `}`; document.write(formattedJSONString); 
 .json-key { color: blue; margin-left: 8px; font-family: monospace; } .value { font-family: monospace; } 

Here's how you'd work it: 这是您的工作方式:

(?<=")    (.*?)    (?=":)
   1        2         3
  1. Lookbehind to require a quote before the text you want. 后方需要在您想要的文本前加引号。
  2. What you actually want to capture. 您实际想要捕获的内容。
  3. Lookahead to require a quote and colon be present after your text. 先行要求在文本后加引号和冒号。

Then, replace it with: 然后,将其替换为:

<span style="color: red;">$1</span>

Here is a demo 这是一个演示

I've used recursion for this.我为此使用了递归。

JSON object JSON 对象

let json = {
  foo: "bar",
  age: 23,
  foo2: {
    hello: "world",
    world: "hello"
  },
};

This function will iterate over the JSON object and add its keys and values into span tags and seprate them with JSON syntax tokens此函数将遍历 JSON 对象并将其键和值添加到 span 标签中,并使用 JSON 语法标记分隔它们

 let json = { foo: "bar", age: 23, hi: "bye", bar: { hi: "now", now: "hi" } }; let stylizedJson = "<span class='syntax'>{</span><br/>" + createFormattedJSON(json,24) + "<br/><span class='syntax'>}</span>"; document.write(stylizedJson); function createFormattedJSON(data, margin) { var formattedJson = ''; Object.entries(data).forEach(([key, value]) => { formattedJson += `<span style='margin-left:${margin}px;' class='json-key'>"${key}"</span><span class="syntax" ${this.scope}>:</span>` if (typeof value == "object") { formattedJson += `<span class='syntax'>{</span><br/>` formattedJson += this.createFormattedJSON(value, margin + 12) formattedJson += `<br/><span style='margin-left:${margin}px;' class='syntax'>}</span>` } else { if (Object.keys(data).reverse()[0] != key) formattedJson += `<span class='value'>"${value}"</span><span class="syntax">,</span><br/>` else formattedJson += `<span class='value'>"${value}"</span>` } }) return formattedJson; }
 .json-key { color: #7A01FF; margin-left: 10px; font-family: Consolas; } .value { color: #F9D372; font-family: Consolas; } .syntax { color: #EEEEEE; font-family: Consolas; } body { background-color: #232932; padding: 12px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/typescript/4.5.4/typescript.min.js"></script>

Thanks谢谢

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

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