Code tagged with parsing

highlight multiple terms, escape html

Posted by Chad Humphries over 2 years ago
# This will search and replace multiple terms with an arbitrary string 
# but ignore any characters inside angle brackets inside the string 
# eg "This is spantastic!" => This is spantastic! 

def search_highlight text, phrases 
  return text if phrases.blank? 
  phrases.each do |phrase| 
    phrase = Regexp.escape(phrase) 
    unless text.nil? 
      text = text.gsub /((?:\G|>)[^<>]*?)(#{phrase})/i, '\1\2' 
    end 
  end 
  return text 
end
Language Ruby / Tagged with html, parsing