简体   繁体   English

仅从JS中的文件中获取YAML文本

[英]Grabbing only the YAML text from a file in JS

Suppose I have a file that has random text mixed with yaml text. 假设我有一个包含随机文本和yaml文本的文件。 The yaml text is always between a --- and a ... . yaml文本始终在---...

Goal: My goal is to take a string (taken from such a file): 目标:我的目标是获取一个字符串(从这样的文件中获取):

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

Some random text that's not yaml.

---
more: "yaml"
...

And output a string that contains only the yaml parts (including the --- and ... delimiters): 并输出包含yaml部分(包括---...分隔符)的字符串:

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

How does one do this? 如何做到这一点?

This can be easily done with String.prototype.match + Array.prototype.join: 这可以通过String.prototype.match + Array.prototype.join轻松完成:

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

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

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