反应Javascript显示/解码unicode字符 [英] React Javascript displaying/decoding unicode characters

查看:62
本文介绍了反应Javascript显示/解码unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在unicode中有一个我需要转换的字符串。我需要将带有\\\ó的字符串渲染到ó。这是一个例子,它应该与所有其他类型的字符一起发生,á,í,ú...

I have a string in unicode that i need to convert. I need to render the string with \u00f3 to ó. This is an example, it should happen with all other types of characters, á, í, ú...

我有以下基本代码:
< a href =https://jsfiddle.net/dddf7o70/ =noreferrer> https://jsfiddle.net/dddf7o70/

我需要转换

<Hello name="Informaci\u00f3n" />

进入

Información


推荐答案

如果你必须与之合作无论出于什么原因,那些 \u .... 代码中的代码而不是真正的字母,然后将它们转换为数字,然后使用String.fromCharCode ()将这些数字变成真实的字母。我们可以使用regexp替换为处理函数:

If you have to work with strings that have, for whatever reason, those \u.... codes in them instead of being real letters, then convert them to numbers, and then use String.fromCharCode() to turn those numbers into real letters. We can use a regexp replace with handler function for this:

function convertUnicode(input) {
  return input.replace(/\\u(\w\w\w\w)/g,function(a,b) {
    var charcode = parseInt(b,16);
    return String.fromCharCode(charcode);
  });
}

var Hello = React.createClass({
  getInitialState: function() {
    return {
      name: convertUnicode(this.props.name)
    };
  },
  render: function() {
    return <div>Hello {this.state.name}</div>;
  }
});

React.render(
  <Hello name="Informaci\u00f3n" />,
  document.getElementById('container')
);

小提琴: https://jsfiddle.net/dddf7o70/4/

这篇关于反应Javascript显示/解码unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆