aidiah aidiah - 1 year ago 59
Ruby Question

Using regex with highlight

I'm trying to highlight all lines in

that start with $ using regex

<%= highlight(post.comment, /^\$.*$/) %>

However this returns the error
no implicit conversion of Regexp into String

How can I format this to match every string starting with $?

$highlight test
$highlight test

Answer Source

Your code works for me on Rails 4.2 with Ruby 2.2.5, assuming that post.comment is a string.

As an alternative, You can also try the following using sub:

raw(post.comment.gsub(/^\$.*$/) { |item| "<mark>#{item}</mark>" })