What is a JavaScript unescape equivalent using Ruby?
I want to unescape the following string:
'\u00020\u0002Standard\u00023\u0002Doe John\u000169\u0002\u0010\u0002Lo开发者_如何学Gorem\u0002\u0011\u0002Ipsum\u0002\u0014\u0002'
Javascript's unescape
function works OK, however how can I unescape it in Ruby?
If I unescape
the string above, it will result in "0Standard3Doe John69LoremIpsum
".
It seems to me that you're using Ruby 1.8, so you have poor encoding support.
In Ruby 1.8 the jcode library could but go here to find some 3rd party libraries that will help you. Ruby 1.9 has a better support for encoding, in fact It's one of the major changes of this release.
If you are in complete control over your application maybe you can encode your string without \u00020 kind of utf-8 entities, directly at Its source. This way you will avoid any regex parsing and CGI.unescapeHTML will work.
For reference and better understanding of unicode support in ruby JEG2 series will give you everything you need.
this can help https://github.com/brianmario/escape_utils
Unescaping
javascript = `curl -s http://code.jquery.com/jquery-1.4.2.js`
escaped_javascript = EscapeUtils.escape_javascript(javascript)
EscapeUtils.unescape_javascript(escaped_javascript) == javascript # => true
精彩评论