0

我写了一小段代码来从 rss 页面检索新闻。这是代码:

this.loadRecentNews = function loadRecentNews() {
            $.get("http://rss.nytimes.com/services/xml/rss/nyt/GlobalHome.xml", function (data) {
                $(data).find("item").each(function () {
                    var el = $(this);

                    console.log("------------------------");
                    console.log("Title      : " + el.find("title").text());
                    console.log("Link     : " + el.find("link").text());
                    console.log("Description: " + el.find("description").text());
                    console.log("Date: " + el.find("pubDate").text());

                });
            });

        };

这是输出:

Title : Example of title

Link : http://www.example.com

Description : Example of <<bb>>Description</b> containing <> tag..

Date : Example of date

我的问题是我只想提取描述值的文本来构建一个包含此文本的新 Json 对象。

我怎样才能只提取没有 <> 值的文本?

4

1 回答 1

0

看到你正在使用 jQuery,你可以使用它的.text()方法。

var beforeText = "Example of <b>Description</b>" ;
var afterText = $("<p>").html(beforeText).text() ;

这使用 jQuery 创建一个新的 HTML 元素(从未添加到页面中)并更新其 innerHTML,然后用于.text()仅获取文本。

另一个(风险更高的)选项是一个正则表达式,用于替换空字符串之间<的任何内容:>

var afterText = beforeText.replace(/<[^>]*>+/g, "")
于 2013-04-18T12:08:45.013 回答