我试图突出显示 post.comment
中的所有行使用正则表达式以 $ 开头
<%= highlight(post.comment, /^\$.*$/) %>
但是这会返回错误 no implicit conversion of Regexp into String
我如何格式化它以匹配每个以 $ 开头的字符串?
$highlight test
test
$highlight test
最佳答案
假设 post.comment
是一个字符串,您的代码适用于 Rails 4.2 和 Ruby 2.2.5。
作为替代方案,您还可以使用 sub
尝试以下操作:
raw(post.comment.gsub(/^\$.*$/) { |item| "<mark>#{item}</mark>" })
关于ruby-on-rails - 使用带突出显示的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37234393/