node.js - JS Doc字符串的正则表达式匹配描述部分
问题描述
我正在尝试匹配 JS 文档字符串的描述部分(不带 @ 符号的行)。这里有几个文档字符串的例子。
/**
* This is a doc string
* @param withArg description of withArg
* @param withArg2 description of withArg2
*/
/**
* This is an other doc string
* This is second line of description.
*/
这似乎在正则表达式编辑器中工作:
/\*\S*(.*?)(@)/
见:https ://regexr.com/4dfbn
但在 javascript 中: https ://jsbin.com/qekisogula/1/edit?html,js,console
关于发生了什么的任何想法?
非常感谢
更新:预期输出
示例 1:
This is an other doc string
示例 2
This is an other doc string
:,This is second line of description.
解决方案
我实际上会采用逐行读取文件并使用一些基本解析逻辑的方法:
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('file.in')
});
var start = false;
lineReader.on('line', function (line) {
if (line.startsWith("/**") {
start = true;
}
else if (line.startsWith("*/")) {
start = false;
}
else if (start && !line.includes("@param")) {
console.log("doc string: ", line);
}
});
这里的逻辑是我们使用布尔标志start
来跟踪我们是否在带有文档字符串的注释中。击中/**
标志打开,击中*/
标志关闭。如果遇到不包含的行@param
,则将其回显到控制台。
推荐阅读
- assembly - nasm 指令预期 inb outb
- javascript - 使用 wordpress rest api 发送评论元
- laravel - 帕斯卡案例属性上的 Laravel 访问器
- windows - For循环:未考虑变量
- python - 如何解决不匹配数组和列表之间相乘的广播问题
- python - 基于前一个元素的列表理解 if 语句
- angular - Angular - 类型“对象”在执行 http get 时缺少类型中的以下属性
- php - 如何在 Woocommerce 中移动可变产品价格字段
- python-3.x - TypeError:无法在 Login.interactive 处读取未定义的属性“id”
- opengl - 如何在 Colab 中运行moderngl?