aidiah aidiah - 6 months ago 18
Ruby Question

Using regex with highlight

I'm trying to highlight all lines in

post.comment
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
test
$highlight test

Answer

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>" })