2017-07-19 2 views
0

Ich versuche, Kommentare aus JS-Dateien zu lesen. Ich lese die Datei und Vergleiche mit der Regex aber für das Leben von mir kann nicht herausfinden, wie nur Kommentare lesen, die mit @SimpleSwagger beginnen. Jede Hilfe würde sehr geschätzt werden.Node Regex Match Kommentare in der Datei

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

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

Antwort

0

vielleicht ist es das, was Sie wollen zunächst den Inhalt der Datei in Array wie folgt ändern:

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 

Modell:
Name: Auto