如何从JSON中删除HTML实体?

时间:2015-07-02 18:40:39

标签: javascript regex json angularjs

我根据a simple RSS feed app using AngularJS创建this JSON data

我设法收到帖子title和帖子content

但是,在处理content数据时遇到了问题。我想删除里面的所有HTML实体,使它看起来像Pocket app这样:

enter image description here

我只想:

  1. 纯文本内容(来自Feed源的相同段落结构)
  2. 居中对齐的图像
  3. 超链接
  4. 无序和有序列表项
  5. 视频(embed标签的结果 - 如果有的话)
  6. 我读到了关于ngSanitize但我无法理解如何使用它来实现我想要的目标。

    怎么做?

1 个答案:

答案 0 :(得分:2)

如果你想从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);