0
我想写一个函数,它接受一个字符串并返回一个包含所有块注释内容的数组。例如:如何使用正则表达式从字符串中提取块注释?
var text = 'This is not a comment\n/*this is a comment*/\nNot a comment/*Another comment*/';
var comments = getComments(text);
和“评论”将与值的数组:
['this is a comment', 'Another comment']
我试图与此代码:
function getComments(text) {
var comments,
comment,
regex;
comments = [];
regex = /\/\*([^\/\*]*)\*\//g;
comment = regex.exec(text);
while(comment !== null) {
skewer.log(comment);
comments.push(comment[1]);
comment = regex.exec(text);
}
return comments;
}
的问题是,如果有一个其*或/里面的评论,它不匹配
这只适用于* most *但不是所有的情况。以'var a =“/ *”; var b =“* /”;'例如 - 不应该提取任何评论。 – nhahtdh 2014-11-25 02:40:53