# Unescaping html

**URL:** <https://www.ruby-forum.com/t/unescaping-html/146141>\
**Category:** Ruby\
**Created:** [August 18, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/unescaping-html/146141 "2008-08-18T13:45:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [August 18, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/unescaping-html/146141/1 "2008-08-18T13:45:48Z")

</div>

Hi,  
Is there a method or any library which allows me to unescape encoded  
html. For example If I have hi%5Bthere%5D  
I should get hi[there]

vivek

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [August 18, 2008, 2:18pm UTC](https://www.ruby-forum.com/t/unescaping-html/146141/2 "2008-08-18T14:18:02Z")

</div>

Vivek wrote:

> Hi,  
> Is there a method or any library which allows me to unescape encoded  
> html. For example If I have hi%5Bthere%5D  
> I should get hi[there]
> 
> vivek

I believe you’re looking for the CGI::unescape method. If for some  
reason you don’t have that, you can do a simple gsub like this, though  
it can fail in many ways and only handles hex entities.

gsub(/%([[:xdigit:]]{2})/) {|c| $1.hex.chr }

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [August 18, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/unescaping-html/146141/3 "2008-08-18T14:35:59Z")

</div>

Vivek wrote:

> Hi,  
> Is there a method or any library which allows me to unescape encoded  
> html. For example If I have hi%5Bthere%5D  
> I should get hi[there]
> 
> vivek

encoded _HTML_ ? what you will need is hpricot (which is amazing 🙂

require ‘rubygems’  
require ‘hpricot’

Hpricot(“hi\>there\<”, :xhtml\_strict =\> true).to\_plain\_text # =\>  
“hi\>there\<”

But! from your example seems like you don’t want to unescape html but  
the url:

require ‘cgi’

CGI.unescape “hi%5Bthere%5D”  
=\> “hi[there]”

Greets!

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [August 18, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/unescaping-html/146141/4 "2008-08-18T14:45:36Z")

</div>

On Aug 18, 1:41 pm, Vivek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a method or any library which allows me to unescape encoded  
> html. For example If I have hi%5Bthere%5D  
> I should get hi[there]

I think you mean encoded URL, not encoded HTML. For URL encoding, you  
can use URL.unescape, or CGI.unescape as Michael sugests.

HTML escaping is a bit more complex, but the CGI has some methods,  
e.g. CGI.escapeHTML/CGI.unescapeHTML.

---

<div class="post-metadata">

**Author:** ![vivek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@vivek](https://www.ruby-forum.com/u/vivek)\
**Post date:** [August 18, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/unescaping-html/146141/5 "2008-08-18T15:20:48Z")

</div>

Thanks for the replies , actually I want to unescape some form  
parameters which contain [and] .  
The CGI.unescape works…Is it a superset of what URL.unescape does?
