gpt4 book ai didi

javascript - 如何从 RSS 源中删除 HTML 标签?

转载 作者:行者123 更新时间:2023-12-02 17:27:50 26 4
gpt4 key购买 nike

我正在使用Google Feed APItumblr feed 中提取博客条目.

我已经能够提取内容,但输出带有 html 标签,如下所示:

<p>I remember one day asking one of my mentors James if he ever got nervous around people. James replied, “Only when I need something from them.”</p>

代码很简单,如下:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("feeds", "1");

function initialize() {
var feed = new google.feeds.Feed("http://adriennetran.tumblr.com/rss");
feed.load(function(result) {

if (!result.error) {
var container = document.getElementById("feed");

for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
window.content = document.createTextNode(entry.content);
container.appendChild(content);
}
}
});
}


google.setOnLoadCallback(initialize);

</script>

我尝试编写一个函数来删除以 < 开头的所有内容像这样:

content_array = content.split(" ");

for (i=0; i < content_array.length; i++){
if ((content_array[i].split(""))[0] == "<"){
content_array.splice(i, 1);
}
}

content2 = content_array.toString();

但我得到了 Uncaught TypeError: undefined is not a function错误,因为 contentobject而不是string因此我无法调用 content.split(" ") .

我尝试转换为字符串,但这是控制台的输出

typeof(content)
> "object"

c2 = content.toString()
> "[object Text]"

有人对如何操作从 RSS 检索的元素有任何想法吗?

最佳答案

让我们看看

var regExString = /(<([^>]+)>)/ig; //create reg ex and let it loop (g)
contentString = content.textContent // get text from node (no longer an object but string.

contentString = contentString.replace(regExString, "") //find all tags and delete them.

关于javascript - 如何从 RSS 源中删除 HTML 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27596228/

26 4 0