Convert escaped codepoint to unicode character

六眼飞鱼酱① 提交于 2019-12-21 12:16:10

问题


I am trying to take a chunk of JSON that has strings which contain the literal characters \u009e and I would like to convert those characters to its associated single unicode character, in this case é.

I use curl or wget to download the json which looks like:

{ "name": "Kitsun\u00e9" }

And need to translate this in Vim to:

{ "name": "Kitsuné" }

My first thought was to use Vim's iconv, but it does not evaluate the string as a single character and just returns the input.

let code = '\u00e9'
echo iconv(code, "UTF-8", "UTF-8")
" Prints \u00e9

I want to eventually use something like

%s;\\u[0-9abcdef]*;\=iconv(submatch(0),"UTF-8", "UTF-8");g

回答1:


this line works for your example:

s#\\u[0-9a-f]*#\=eval('"'.submatch(0).'"')#

or

s#\v\\u([0-9a-f]{4})#\=nr2char(str2nr(submatch(1),16))#


来源:https://stackoverflow.com/questions/21076598/convert-escaped-codepoint-to-unicode-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!