我有一个类似 blah blah::e||1f1e6-1f1ea::blah blah
的字符串,我想将其转换为 blah blah 🇦🇪 blah blah
。
🇦🇪
可以写成\u{1f1e6}\u{1f1ea}
。
我使用了下面的代码,但它转换为 \u{1f1e6}\u{1f1ea}
这个字符串,而不是符号。
str.replace(/\:\:e\|\|(.*?)\:\:/g, function(x) { return x.replace(/-/g, '\}\\u\{'); }).replace(/\:\:e\|\|(.*?)\:\:/g, "\\u\{$1\}" );
最佳答案
1f1e6
和 1f1ea
是该字符的十六进制 Unicode 代码点。您可以使用 String.fromCodePoint
,但您需要为 IE 使用 PolyFill:
text.replace(/::e\|\|([a-f0-9\-]+)::/g, function(match, text) {
var code_points = text.split('-').map(function(n) {
return parseInt(n, 16);
});
return String.fromCodePoint.apply(undefined, code_points);
});
关于javascript - 将字符串替换为 unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36973086/