2017-07-19 70 views
0

我想从js文件读取评论。我正在阅读文件并与正则表达式进行比较,但是对于我的生活而言,无法弄清楚如何只读取以@SimpleSwagger开头的注释。任何帮助将不胜感激。节点正则表达式匹配文件中的评论

var fileContent = fs.readFileSync(file, { encoding: 'utf8' }); 
var regexResults = fileContent.match(jsDocRegex); 

/** 
* @SimpleSwagger 
* Model: 
* name: Car 
*/ 

回答

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 

型号:
名称:汽车