【问题标题】:Grabbing only the YAML text from a file in JS仅从 JS 文件中获取 YAML 文本
【发布时间】:2016-02-08 21:07:45
【问题描述】:

假设我有一个包含随机文本和 yaml 文本的文件。 yaml 文本始终介于 ---... 之间。

目标:我的目标是获取一个字符串(取自这样的文件):

---
some: "yaml"
some: "more yaml"
...

Some random text that's not yaml.

---
more: "yaml"
...

并输出一个包含 yaml 部分(包括---... 分隔符)的字符串:

---
some: "yaml"
some: "more yaml"
...    
---
more: "yaml"
...

如何做到这一点?

【问题讨论】:

    标签: javascript node.js yaml


    【解决方案1】:

    这可以通过 String.prototype.match + Array.prototype.join 轻松完成:

    function extractYaml(string) {
        return string.match(/(---(.|\n)*?\.\.\.\n)/g).join('');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多