RegEx for jsDoc注释 [英] RegEx for jsDoc comments
问题描述
找不到任何可用的RegExp示例来匹配文本文件中的所有jsDoc注释块.
基本上,它需要找到满足以下规则的所有块:
- 以
/**
开头,然后是一个或多个换行符 - 以
*/
结尾
- 在该块中可以包含任何内容,包括代码示例和HTML.
我尝试了 jsdoc-regex 中的代码,但是没有用:>
/[^\S\r\n]*\/(?:\*{2})([\W\w]+?)\*\/([\W\w]+?)?(?=\s+\/\*{1,2}|$)/g;
注意:
我正在解析通用文本文件,不一定是JavaScript,因此我不能使用Esprima之类的JS解析器.
更新:
为了重新回答我的问题,我被困在试图定义一个RegEx规则,该规则说表达式必须以*/
结尾,而在它之前可以有任何内容(在块内),即在这种情况下表示任何含义除了*/
.
解决方案:
感谢您使用 @ m93a 的答案,他的实现已成为建议,选项忽略.>
构建表达式:教程
-
/ /
–空表达式 -
/
\/\*\*
/
–匹配/**
(转义) -
/
\/\*\*
\s*\n
/
–后面是换行符(空格字符可能在前面) -
/
\/\*\*
\s*\n
\*\/
/
–匹配*/
(转义) -
/
\/\*\*
\s*\n
(
)
\*\/
/
–添加组 -
/
\/\*\*
\s*\n
(
[^\*]
)
\*\/
/
–该组可以包含除*
以外的任何内容> -
/
\/\*\*
\s*\n
(
[^\*]
|
(
)
-
/
\/\*\*
\s*\n
(
[^\*]
|
(
\*
)
)
\*\/
/
–有*
-
/
\/\*\*
\s*\n
(
[^\*]
|
(
\*
(?!\/)
)
)
\*\/
/
–但后面没有/
-
/
\/\*\*
\s*\n
(
[^\*]
|
(
\*
(?!\/)
)
)
*
\*\/
/
–重复整个过程 -
/\/\*\*\s*\n([^\*]|(\*(?!\/)))*\*\//
– Tadaa!漂亮,不是吗?
Can't find any usable RegExp example to match all jsDoc comment blocks within a text file.
Basically, it needs to find all blocks that satisfy the following rules:
- Starts with
/**
, followed by one or more line breaks - Ends with
*/
- Within the block there can be anything, including code examples and HTML.
I tried the code from jsdoc-regex, but it didn't work:
/[^\S\r\n]*\/(?:\*{2})([\W\w]+?)\*\/([\W\w]+?)?(?=\s+\/\*{1,2}|$)/g;
NOTE:
I am parsing generic text files, not necessarily JavaScript, so I can't use JS parsers like Esprima.
UPDATE:
To rephrase my question, I'm stuck trying to define a RegEx rule that says the expression must end with */
, while there can be anything prior to it (within the block), i.e. by anything in this case means anything except */
.
SOLUTION:
Thanks to you the answer by @m93a, his implementation has become a valuable addition to decomment, option ignore.
Build your expression: Tutorial
/ /
– empty expression/
\/\*\*
/
– match/**
(escaped)/
\/\*\*
\s*\n
/
– followed by a line break (whitespace chars may be preceding)/
\/\*\*
\s*\n
\*\/
/
– match*/
(escaped)/
\/\*\*
\s*\n
(
)
\*\/
/
– add a group/
\/\*\*
\s*\n
(
[^\*]
)
\*\/
/
– the group may consist of anything but*
/
\/\*\*
\s*\n
(
[^\*]
|
(
)
)
\*\/
/
– or another group/
\/\*\*
\s*\n
(
[^\*]
|
(
\*
)
)
\*\/
/
– where there is an*
/
\/\*\*
\s*\n
(
[^\*]
|
(
\*
(?!\/)
)
)
\*\/
/
– but isn't followed by a/
/
\/\*\*
\s*\n
(
[^\*]
|
(
\*
(?!\/)
)
)
*
\*\/
/
– repeat the whole thing/\/\*\*\s*\n([^\*]|(\*(?!\/)))*\*\//
– Tadaa! Beautiful, isn't it?
这篇关于RegEx for jsDoc注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!