gpt4 book ai didi

Javascript Regex匹配JSDoc字符串参数说明

转载 作者:行者123 更新时间:2023-11-29 22:59:03 24 4
gpt4 key购买 nike

给定一个 JSDoc 字符串,例如:

*\n * 这是一个文档字符串\n * @param withArg withArg 的描述\n * @param withArg2 withArg2 的描述\n

我正在尝试提取在 Node 8 上运行的应用程序中的参数名称和描述。

这个正则表达式:/@param(\s?\w*)(.*?)\\n/ 似乎在正则表达式编辑器中工作。看 https://regex101.com/r/uJz0Km/2/ .

但是当这样跑的时候:

const matchParamsAndDescriptionRegex = /@param(\s?\w*)(.*?)\\n/g;
const matches = matchParamsAndDescriptionRegex.exec(value);

它返回 null。你们能看出哪里出了问题吗?

非常感谢

最佳答案

您的原始字符串中必须有常规的 LF 符号,而不是 \n 的双字符序列。

使用

/@param\s*\w*(.*)/g

参见 regex demo在哪里

  • @param - 匹配 @param
  • \s*\w* - 匹配 0+ 个空格,然后匹配 0+ 个单词字符
  • (.*) - 将任何零个或多个字符捕获到组 1 中。

示例 JS 演示:

var s = "*\n * This is a doc string\n * @param withArg description of withArg\n * @param withArg2 description of withArg2 \n"
var rx = /@param\s*\w*(.*)/g;
var res=[], m;
while (m = rx.exec(s)) {
res.push(m[1].trim());
}
console.log(res);

关于Javascript Regex匹配JSDoc字符串参数说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56003147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com