| Next | 3.6 Regular expression example | Prev |
def unescapeHTML(string)
str = string.dup
str.gsub!(/&(.*?);/) {
case $1
when /\Aamp\z/i then '&'
when /\Aquot\z/i then '"'
when /\Agt\z/i then '>'
when /\Alt\z/i then '<'
when /\A#(\d+)\z/ then Integer($1).chr
when /\A#x([0-9a-f]+)\z/i then $1.hex.chr
end
}
str
end
p unescapeHTML("1<2 && 4>3") # 1<2 && 4>3
p unescapeHTML(""A" = A = A") # "A" = A = A
| Next | Ruby for Perl programmers | Prev |