我正在尝试提取注释以使用它们。
所以,如果我有
var a = 'foo';
// this comment should stay here
/* Description: I need to be printed in a section */
/* Usage: I need to be printed in another section
and I have multiple lines
*/
预期结果1:
var a = 'foo';
// this comment should stay here
预期结果2:
Description: I need to be printed in a section
预期结果3:
Usage: I need to be printed in another section
and I have multiple lines
所以我基本上想要的是3个变量
var code = str.replace(regex to remove comments)
var description = str.match(regex to match comments with description)
var usage = str.match(regex to match comments with usage)
我可以根据需要格式化评论。
顺便说一句。我在一个咕噜咕噜的脚本中这样做,以防重要
var str = grunt.file.read('myfile.filetype')
这是一个实际的例子
@import '../helpers/_prefix';
// MIXIN: .animation-delay
.animation-delay(@values) {
@vendorPrefixes: -webkit-, -moz-, '';
@prop: animation-delay;
// http://caniuse.com/#search=animation
.prefix();
}
/* Description: Defines the delay of an animation */
/* // Usage:
.animation-delay(200ms)
*/
我还希望删除/*
和*/
代码块应输出
@import '../helpers/_prefix';
// MIXIN: .animation-delay
.animation-delay(@values) {
@vendorPrefixes: -webkit-, -moz-, '';
@prop: animation-delay;
// http://caniuse.com/#search=animation
.prefix();
}
最后一个(用法)应输出
// Usage
.animation-delay(200ms)
或
.animation-delay(200ms)
答案 0 :(得分:0)
你可以这样做:
m = str.match(/\/\/.*/g);
var code = m ? m[0] : "";
m = str.match(/\/\*.*?Description: *([\s\S]*?)\*\//);
var description = m ? m[1] : "";
m = str.match(/\/\*.*?Usage: *([\s\S]*?)\*\//);
var usage = m ? m[1] : "";