简体   繁体   中英

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.

The CGI library is one option:

require 'cgi'

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM