2013-02-16 34 views
-1

我有這樣的代碼註釋塊..正則表達式來讀取代碼註釋

/** 
* 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

告訴我們你試過了什麼? – 2013-02-16 14:38:52

+1

你應該精確地確定你想要的。也許爲什麼很多關於C++風格評論的正則表達式的文章沒有回答你的問題。 – 2013-02-16 14:39:56

+0

i tired this/\ *([^ *] | [\ r \ n] |(\ * +([^ * /] | [\ r \ n]))* \ * – 2013-02-16 14:42:38

回答

1

我假設你只是在尋找多行註釋,這裏是一個完全的。

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

與您的第一個示例匹配的評論塊的正則表達式是/\/\*\*[\s\S]*?\*\//。在提取整個評論後,你可以做一些額外的解析。

1

我相信這就是你要找的。

  • 首先它尋找/**
  • 然後,它只要在評論中的任意內容匹配,因爲我們沒有找到*/
  • 最後,它會尋找*/結束

regexpal這裏測試。

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