bsteo
bsteo

Reputation: 1779

Ruby unescape HTML string

Any idea how I can unescape the following string in Ruby?

C:\inetpub\wwwroot\adminWeb

to

C:\inetpub\wwwroot\adminWeb

or to

C%3A%5Cinetpub%5Cwwwroot%5CadminWeb

Tried with URI.decode with no success.

Upvotes: 7

Views: 5528

Answers (3)

Motine
Motine

Reputation: 1862

An alternative is using the standard lib's URI module:

require 'uri'
URI.unescape "C%3A%5Cinetpub%5Cwwwroot%5CadminWeb" # => "C:\\inetpub\\wwwroot\\adminWeb"

EDIT This is answer is obsolete. Please check this thread.

Upvotes: 1

goodniceweb
goodniceweb

Reputation: 166

One more variant is HTMLEntities

HTMLEntities.new.decode "C:\inetpub\wwwroot\adminWeb"             
# => "C:\\inetpub\\wwwroot\\adminWeb"

I prefer to use it because it deals with rare cases aså and — which CGI.unescapeHTML does not

Upvotes: 6

Yu Hao
Yu Hao

Reputation: 122383

The CGI library is one option:

require 'cgi'

CGI.unescapeHTML('C:\inetpub\wwwroot\adminWeb')
# => "C:\\inetpub\\wwwroot\\adminWeb"

Upvotes: 22

Related Questions