【问题标题】:How to decode special character or HTML entities in react native or JS?如何在 react native 或 JS 中解码特殊字符或 HTML 实体?
【发布时间】:2018-12-23 21:03:51
【问题描述】:

我从具有 HTML 实体的后端获取字符串。

‘’“”各自。

我使用了不同的功能,例如:-

var map = { amp: '&', lt: '<', gt: '>', quot: '"', '#039': "'"};

var output = newsTitle.replace(/&([^;]+);/g, (m, c) => map[c]);

输出是解析后的字符串,但它不能替换 JavaScriptReact Native 中的。任何帮助将不胜感激。

编辑:-

我实际上是在传递这个文本

<Text numberOfLines={2}>
{output}
 </Text>

【问题讨论】:

  • 纯 JS 对我来说很好用:jsfiddle.net/khrismuc/be6ahnwm
  • @ChrisG 在此我们必须静态传递地图中的每个 html 实体。我们是否有可以动态替换它的东西。
  • 是的,dangerouslySetInnerHTML={{ __html: newsTitle }}
  • 我添加了一些代码,即我如何传递文本。您能否解释一下如何使用您提供的属性。 @ChrisG
  • 我把这两个选项都放在了我在第一条评论中链接的小提琴中。

标签: javascript reactjs parsing react-native html-parsing


【解决方案1】:

你可以使用这个库https://github.com/mdevils/node-html-entities

const Entities = require('html-entities').XmlEntities;

const entities = new Entities();
console.log(entities.decode('&lt;&gt;&quot;&apos;&amp;&copy;&reg;&#8710;')); // <>"'&&copy;&reg;∆

【讨论】:

    【解决方案2】:

    $('form').submit(function() {
      var theString = $('#string').val();
      var varTitle = $('<textarea />').html(theString).text();
      $('#output').text(varTitle);
      return false;
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form action="#" method="post">
      <fieldset>
        <label for="string">Enter a html-encoded string to decode</label>
        <input type="text" name="string" id="string" />
      </fieldset>
      <fieldset>
        <input type="submit" value="decode" />
      </fieldset>
    </form>
    
    <div id="output"></div>

    【讨论】:

    • 我想在 react native 中解码。不知道如何在 react native 中编写 jQuery
    猜你喜欢
    • 2017-07-05
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    相关资源
    最近更新 更多