我正在尝试从js文件中读取注释。我正在阅读文件并与正则表达式进行比较,但对于我的生活,我无法弄清楚如何只读取以@SimpleSwagger
开头的注释。任何帮助将不胜感激。
var fileContent = fs.readFileSync(file, { encoding: 'utf8' });
var regexResults = fileContent.match(jsDocRegex);
/**
* @SimpleSwagger
* Model:
* name: Car
*/
答案 0 :(得分:0)
也许这就是你想要首先将文件内容更改为数组,如下所示:
var fs = require('fs')
var fileContent = fs.readFileSync("peking.txt", { encoding: 'utf8' });
var splitSimpleSwagger=fileContent.split(/@SimpleSwagger/).filter(function(token){
return token != ""
})
console.log(splitSimpleSwagger)
//this will return 2 array which is the word above string @SimpleSwagger and the word
//after @SimpleSwagger like this:
//[ 'var fileContent = fs.readFileSync(file, { encoding: \'utf8\' });\nvar
//regexResults = fileContent.m
//atch(jsDocRegex);\n\n /**\n * ',
//'\n * Model:\n * name: Car\n */' ]
//so if you want to get only this '\n * Model:\n * name: Car\n */' just do this:
console.log(splitSimpleSwagger[1])
//then you will get the contain of the comment after word '@SimpleSwagger' which is
//'\n * Model:\n * name: Car\n */'
//and if you want to clean that up just do this:
console.log(splitSimpleSwagger[1].split("*").join("").replace("/",""))
//it will return
型号:
名称:汽车