正则表达式阅读代码评论

时间:2013-02-16 14:38:00

标签: javascript regex

我有像这样的代码评论块..

/**
 * This method provide inheritance to the object supplied; this method inherit the 
 * public methods from the Parent class to the Child class. this also provide
 * multiple inheritance, in which the method ambiguity is solved by the overriding
 * the last inherited class's method.
 * @access public
 * @method inherit
 * @param Object Parent
 * @param Object Child
 * @return Object
 */

并忽略

之类的评论
/* This is a test comment for testing regex. */

我尝试了很多在javascript中使用正则表达式来获取此评论并​​且每次都失败了。任何人都可以建议使用这个或任何其他解决方案的正则表达式来提取注释块。

3 个答案:

答案 0 :(得分:1)

与第一个示例匹配的注释阻止的正则表达式是/\/\*\*[\s\S]*?\*\//。提取整个注释后,您可以进行一些额外的解析。

答案 1 :(得分:1)

我假设你只是在寻找多行注释,这里就是那个。

\/[*]+[\n\r][\s\S]+?[\n\r] *[*]+\/

答案 2 :(得分:1)

我相信这就是你要找的东西。

  • 首先查找/**
  • 然后,只要我们找不到*/
  • ,它就会匹配评论中的任何内容
  • 最后,它会查找*/结束

regexpal进行了测试。

/\*{2}([^\*]|\*(?!/))*\*/