2014-11-23 38 views
0

我在爲我的項目使用ruby 1.9.1。 所以我需要從字符串中刪除一些評論。 如:#encoding:utf-8在軌道上不工作ruby

<!--[if gte mso 9]><xml> 
    <o:OfficeDocumentSettings> 
     <o:RelyOnVML/> 
     <o:AllowPNG/> 
    </o:OfficeDocumentSettings> 
    </xml><![endif]--> 

所以我寫一個函數來text.gsub(/<!--(.*?)-->/s, "")刪除它在幫助和頂部的輔助文件,添加RB # encoding: utf-8,也定義在application.rb中config.encoding = "utf-8"但它似乎並沒有爲我工作。 我得到的錯誤

 ActionView::Template::Error (incompatible encoding regexp match (Windows-31J regexp with UTF-8 string))

在這種情況下你有任何支持嗎?謝謝!

+0

試試這個'text.gsub(/ /m,「」)' – 2014-11-23 03:22:47

+0

@AvinashRaj感謝您的支持。它現在有效。 – 2014-11-23 07:10:03

回答

0

ruby​​中沒有DOTALL修飾符s。只需將s修飾符更改爲m即可使其正常工作。

text.gsub(/<!--(.*?)-->/m, "") 

DEMO

,你也不必捕捉任何字符,只是/<!--.*?-->/m將被罰款。