简体   繁体   中英

# encoding: utf-8 don't working ruby on rails

I'm using ruby 1.9.1 for my project. So I need remove some comments from string which paste from word. such as:

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

So I write a function to remove it in helper with text.gsub(/<!--(.*?)-->/s, "") and added # encoding: utf-8 on the top helper file rb, also define config.encoding = "utf-8" in application.rb but it seem not work for me. The error I get

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

Do you have any support for me in this case? Thanks!

There isn't a DOTALL modifier s in ruby. Just change s modifier to m to make it work.

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

DEMO

And also you don't need to capture any characters , just /<!--.*?-->/m would be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM