2017-07-19 68 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 

型號:
名稱:汽車