I have following method which highlight matched word in text:
# Hightlight matched term
#
# Ex(for term: some):
# "<span class="bold">Some</span> CEO Event"
#
def highlight_matched(matched_word, text)
regex = matched_word.gsub(/\*|\"|\'/, "")
.split(" ")
.map { |s| "\\b#{s}" }.join('|')
text.gsub(/(#{regex})/i, '<span class="bold">\1</span>')
end
Is there a better solution for that?
I am using it here:
"%s %s" %[highlight_matched(title[0..70]), content_tag(:span, caption, class: 'search-type')]
textpure text or HTML? can you add to the question some asserts? – tokland Dec 18 '12 at 20:06HTML– regedarek Dec 18 '12 at 20:18