【问题标题】:How to remove HTML entities from JSON?如何从 JSON 中删除 HTML 实体?
【发布时间】:2015-07-02 18:40:38
【问题描述】:

我正在基于this JSON data 创建a simple RSS feed app using AngularJS

我设法获得了titlecontent 的帖子。

但是,我在处理content 数据时遇到了问题。我想去掉里面的所有 HTML 实体,让它看起来像这样的 Pocket 应用程序:

我只想:

  1. 纯文本内容(与提要源的段落结构相同)
  2. 居中对齐的图片
  3. 超链接
  4. 无序列表和有序列表项
  5. 视频(embed 标签的结果 - 如果有)

我阅读了有关 ngSanitize 的信息,但我无法真正理解如何使用它来实现我想要的。

怎么做?

【问题讨论】:

  • 只解码json
  • 如果你想从内容中去掉所有的 html 标签然后创建一个像这样的正则表达式 str.replace(/]+>/gi, ' ') 并解析内容...您将获得纯文本作为输出。
  • 你想去掉

    标签但仍然保留段落结构是没有意义的,你到底想去掉哪些标签?

  • 如果您想保留段落结构等,您需要保留这些 html 标签。人。尝试ngBindHtml 并使用 css 格式化以满足您的需求。

标签: javascript regex json angularjs


【解决方案1】:

如果您想从 Json 内容中完全删除 html 标签... 然后检查这个..从您的示例 Json 中获取的数据..

输入:

var json = {
    "content": "<p>One of the many new features in <a title=\"WordPress 3.5\" href=\"http://codex.wordpress.org/Version_3.5\">WordPress 3.5</a> is the Iris color picker. <a title=\"Replace Farbtastic color picker\" href=\"http://core.trac.wordpress.org/ticket/21206\"> Iris replaces the, now deprecated, Farbtastic</a> color picker script.  The new Iris color picker is shown off in the Theme Customizer for the Twenty-Twelve theme.</p>\n<p><img class=\"aligncenter size-full wp-image-1634\" src=\"http://rachelbaker.me/wp-content/uploads/2012/11/WordPress-theme-customizer-color-picker2.png\" alt=\"\" /></p>\n<p>As soon as I saw Iris, I fell in love. She is user-friendly, colorful and fun. I found that implementing the new color picker is <a title=\"Adding Farbtastic to WordPress Widgets\" href=\"http://pippinsplugins.com/adding-the-farbtastic-color-picker-to-your-wordpress-widgets/\">very similar to Farbtastic</a>.</p>\n<h3>Iris Color Picker Demo Plugin</h3>\n<p>To use the Iris color picker in a plugin requires:</p>\n<ol>\n<li>Running a version of WordPress that is 3.5 Beta or higher.</li>\n<li>Loading the &#8216;wp-color-picker&#8217; script and style into your plugin options page.</li>\n<li>Adding a text input for your color value to your plugin options page.</li>\n<li>Writing a custom jQuery script to call Iris&#8217;s wpColorPicker method on your color text input field(s).</li>\n</ol>\n<p><strong>How does the code look for implementing steps 2-4?</strong><br />\nI created a demonstration plugin to help answer that. The plugin doesn&#8217;t do anything itself, it is only intended as a guide for developers interested in using the new Iris color picker in a WordPress plugin.</p>\n<p><a class=\"button\" href=\"https://github.com/rachelbaker/iris-color-picker-demo\">View on Github</a></p>\n<p><img class=\"aligncenter size-full wp-image-1635\" src=\"http://rachelbaker.me/wp-content/uploads/2012/11/screenshot-iris-color-picker-demo.jpeg\" alt=\"Iris Color Picker Demo Plugin\" /></p>\n"
};
var someString = 
json.content.replace(/<\/?([a-z][a-z0-9]*)\b[^>]*>/gi, '').
replace(/&#[0-9]+;t/gi,"").replace(/\[/g,"").replace(/\]/g,""); 


console.log(someString);

【讨论】:

  • 谢谢!看起来更好。 jsfiddle.net/02o21rr6/2 但是,如何 1. 保持段落结构 2. 删除“[” 和 “]” 符号 3. 将不转换为不 4. 保留图像?
  • 你的 1,2,3 条件我已经清除了....检查我的代码我已经编辑了............ 4 条件有点复杂必须创建一个条件正则表达式才能跳过 img 标签...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 2011-11-14
  • 1970-01-01
相关资源
最近更新 更多