簡體   English   中英

JavaScript:在 XML 和 JSON 字符串中插入換行符和空格?

[英]JavaScript: Insert line breaks and spaces into XML and JSON strings?

我有以下字符串:

<xml><test myAttr="test123">Test</test></xml>
{"abc":"123","xyz":[{"num":1,"bool":true}]}

我想自動添加換行符和空格/制表符,以便像這樣格式化這些:

<xml>
  <test myAttr="test123">Test</test>
</xml>
{
  "abc": "123",
  "xyz": [
    {
      "num":1,
      "bool":true
    }
  ]
}

然后,我將使用highlightjs.org創建一個 HTML 字符串以顯示在瀏覽器中。

有誰知道 JavaScript 庫或任何語言功能可以輕松做到這一點..?

看看 npm package js-beautify Works for JS, JSON, XML and CSS and you can specify indentation size, max empty lines, brace style etc. There's a Demo here: https://beautifier.io/ .

看起來我可以使用 Prettier API:

https://prettier.io/docs/en/api.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM