How convert these characters?

问题: I'm making a App in React that receive a specific html page and transform into a page in React, but when a put some string in a <Text> these characters are not conver...

问题:

I'm making a App in React that receive a specific html page and transform into a page in React, but when a put some string in a <Text> these characters are not converting:

enter image description here

The representation of &#8212; is .

What these characters are called? Is there a way in React to convert these characters into their corresponding text?


回答1:

These symbols are called HTML Symbol Entities Many mathematical, technical, and currency symbols, are not present on a normal keyboard.

If no entity name exists, you can use an entity number, a decimal, or hexadecimal reference.

A Fast workaround for a single html entity that you know:

    <Text>
      {yourText.replace(/&#8212;/gi, '-')}
    </Text>

You could use this npm module to decode text before rendering:

Example for multiple html entities

const re = /&[^s]*;/gi;
const allHtmlEntities = [];
while ((match = re.exec(youtText)) != null) {
  const htmlEntity = yourText.substring(match.index, 7);
  if (allHtmlEntities.indexOf(htmlEntity) === -1) {
    allHtmlEntities.push(htmlEntity);
  }
}

const entities = new Entities();

for (let i = 0; i < allHtmlEntities.length; i++) {
  const decodedValue = entities.decode(allHtmlEntities[i]);
  yourText = yourText.replace(new RegExp(allHtmlEntities[i], 'g'), decodedValue);
}
  • 发表于 2019-03-31 02:09
  • 阅读 ( 189 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除