2016-10-04 38 views
0

我試圖想出一個正則表達式來匹配字符串中的所有多行註釋。問題是,我不允許以相同格式匹配單行註釋。正則表達式只匹配多行註釋

比賽:

/* This 
is a 
multiline 
comment 
*/ 

不匹配:

/* This is a single line comment */ 
+0

那你試試? –

+0

匹配任何多行註釋,然後檢查它是否包含換行符。 –

+0

修復了答案中的一些錯誤。如果您已經檢查過,請重試。 – Qwertiy

回答

0

類似的東西(有根據語言addidional更正您正在使用):

\/\*((?!\*\/)[^\r\n])*[\r\n]((?!\*\/)[\s\S\r\n])*\*\/ 

測試:

console.log(document.querySelector('textarea').value.match(/\/\*((?!\*\/)[^\r\n])*[\r\n]((?!\*\/)[\s\S\r\n])*\*\//g).join("\n\n"))
textarea { width: 100%; }
<textarea>Match: /* This 
 
is a 
 
multiline 
 
comment 
 
*/ 
 

 
No Match: /* This is a single line comment */ 
 

 
Match: /* 
 
This is a multiline ** comment */ 
 

 
No Match: /* This is a single line comment */ 
 

 
Match: /* This is a multiline ** comment 
 
*/</textarea>