RegEx for jsDoc注释 [英] RegEx for jsDoc comments

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

问题描述

找不到任何可用的RegExp示例来匹配文本文件中的所有jsDoc注释块.

基本上,它需要找到满足以下规则的所有块:

  • /**开头,然后是一个或多个换行符
  • */
  • 结尾
  • 在该块中可以包含任何内容,包括代码示例和HTML.

我尝试了 jsdoc-regex 中的代码,但是没有用:

/[^\S\r\n]*\/(?:\*{2})([\W\w]+?)\*\/([\W\w]+?)?(?=\s+\/\*{1,2}|$)/g;

注意:

我正在解析通用文本文件,不一定是JavaScript,因此我不能使用Esprima之类的JS解析器.

更新:

为了重新回答我的问题,我被困在试图定义一个RegEx规则,该规则说表达式必须以*/结尾,而在它之前可以有任何内容(在块内),即在这种情况下表示任何含义除了*/.

解决方案:

感谢您使用 @ m93a 的答案,他的实现已成为建议,选项忽略.

解决方案

构建表达式:教程

  1. / / –空表达式

  2. / \/\*\* / –匹配/**(转义)

  3. / \/\*\* \s*\n / –后面是换行符(空格字符可能在前面)

  4. / \/\*\* \s*\n \*\/ / –匹配*/(转义)

  5. / \/\*\* \s*\n ( ) \*\/ / –添加组

  6. / \/\*\* \s*\n ( [^\*] ) \*\/ / –该组可以包含除*以外的任何内容

  7. / \/\*\* \s*\n ( [^\*] | ( )

  8. / \/\*\* \s*\n ( [^\*] | ( \* ) ) \*\/ / –有*

    的地方
  9. / \/\*\* \s*\n ( [^\*] | ( \* (?!\/) ) ) \*\/ / –但后面没有/

  10. / \/\*\* \s*\n ( [^\*] | ( \* (?!\/) ) ) * \*\/ / –重复整个过程

  11. /\/\*\*\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

  1. / / – empty expression

  2. / \/\*\* / – match /** (escaped)

  3. / \/\*\* \s*\n / – followed by a line break (whitespace chars may be preceding)

  4. / \/\*\* \s*\n \*\/ / – match */ (escaped)

  5. / \/\*\* \s*\n ( ) \*\/ / – add a group

  6. / \/\*\* \s*\n ( [^\*] ) \*\/ / – the group may consist of anything but *

  7. / \/\*\* \s*\n ( [^\*] | ( ) ) \*\/ / – or another group

  8. / \/\*\* \s*\n ( [^\*] | ( \* ) ) \*\/ / – where there is an *

  9. / \/\*\* \s*\n ( [^\*] | ( \* (?!\/) ) ) \*\/ / – but isn't followed by a /

  10. / \/\*\* \s*\n ( [^\*] | ( \* (?!\/) ) ) * \*\/ / – repeat the whole thing

  11. /\/\*\*\s*\n([^\*]|(\*(?!\/)))*\*\//Tadaa! Beautiful, isn't it?

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

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