Javascript提取注释RegExp [英] Javascript Extract Comments RegExp

查看:98
本文介绍了Javascript提取注释RegExp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的JavaScript文件

I have a Javascript file like that

/**
 * My Comment Line1
 * My Comment Line2
 */
var a = 123;
/**
 * My Comment Line3
 * My Comment Line4
 */
var b = 456;

我正在使用node.js读取文件,并希望提取该文件中的注释。

I am using node.js to read the file and want to extract comments in this file.

我使用此正则表达式

/\/\*\*((?:\r|\n|.)*)\*\//

但是此提取

/**
 * My Comment Line1
 * My Comment Line2
 */
var a = 123;
/**
 * My Comment Line3
 * My Comment Line4
 */

我的程序有一个循环来逐个提取匹配的块。所以我想RegExp提取

My program have a loop to extract matched block one by one. So I want a RegExp to extract

第一个循环

/**
 * My Comment Line1
 * My Comment Line2
 */

第二个循环

/**
 * My Comment Line3
 * My Comment Line4
 */

规则只是注释块以 / **开头并以 * / 结尾。在评论中,所有字符均允许。

The rule is simply that comment block starts with /** and ends with */. Inside a comment, all characters are allowed.

有人可以帮助我吗?谢谢!

Could anyone help me? Thanks!

推荐答案

尝试一下:(几乎所有类型的评论)-现场演示此处: http://regexr.com?30jrh

Try this : (it'll much ANY type of comments) - Live demo here : http://regexr.com?30jrh

(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)

有外观:

这篇关于Javascript提取注释RegExp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆