2012-07-04 49 views
0

我試圖使用正則表達式從.html文件中去除所有html註釋。 我們知道HTML註釋格式是<!-- some comments --> 我已經創造這樣的正則表達式正則表達式嚴格匹配html註釋字符串

/<!--.*-->/gs

它的工作原理,但如果在文件中不止一個註釋塊,這條沒有人到另一個塊,但所有從第一個<!--到最後一個--> Fe

some html tags 
<!-- some comments 1 --> 
some html tags 2 
<!-- some comments 2--> 

它條具有ActionScript語言整個

<!-- some comments 1 --> 
some html tags 2 
<!-- some comments 2--> 

我'代碼。 任何建議都會有幫助。 謝謝!

+0

你需要'+'這一點,沒有'*' – gaussblurinc

回答

5

使用問號,使星號懶:?

/<!--.*?-->/gs 
+0

謝謝!它現在正常工作!你能解釋一下這個問號爲這個正則表達式做些什麼嗎? – igorGIS

+0

問號使星號字符[懶惰](http://en.wikipedia.org/wiki/Regular_expression#Lazy_quantification)。正如「在停止前儘可能少做工」一樣。 – aspidistra

+0

aspidistra非常感謝你的幫助和解釋。 – igorGIS

6

使用這個表達式/<!--.*?-->/gs

相關問題