From: Daniel DeLorme Date: 2007-09-12T08:53:08+09:00 Subject: Re: Substitution with Hash Lee Jarvis wrote: > Thanks that worked well, And no its not single chars, Which is the only > reason i'm doing it this way.. > > I have to split on whitespace (/ /) because spliting on characters would > obviously split the text i want to transform, which means it wont match > if the characters are trailing another word, HTML special chars for > example > > h = {"~" => "~"} If you're just trying to translate numeric html entities it's easy: str.gsub(/&#(\d+);/){ [$1.to_i].pack('U') } If you also want named entities I suggest the htmlentities gems. If it's for a more general case, how about: rx = Regexp.new(hash.keys.map{|k|Regexp.escape(k)}.join("|")) str.gsub(rx){ hash[$&] } Daniel