这是js-yaml库特有的功能,可以把函数和正则表达式转为字符串
#exmaple.yml fn: function() { return 1 } reg: /test/解析上面的yml文件代码如下:
var yaml = require('js-yaml'); var fs = require('fs'); try{ var doc = yaml.load( fs.readFileSync('./example.yml', 'utf8') ); } catch(e){ console.warn(e); }从javaScript对象还原到yaml代码如下:
var yaml = require('js-yaml'); var fs = require('fs'); var obj = { fn: function() { return 1 }, reg: /test/ } try{ fs.writeFileSync('./exmaple.js', yama.dump(obj), 'utf8') } catch(e) { console.log(e); }