[英]extract comments with javascript match and/or replace
我正在嘗試提取注釋以使用它們。
所以如果我有
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)
我可以根據需要格式化注釋。
順便說一句。 如果在重要的情況下,我會在咕unt的腳本中執行此操作
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)
你可以做:
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] : "";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.