【发布时间】:2025-12-31 03:55:12
【问题描述】:
我正在尝试使用 container.innerText || container.textContent 从 HTML 字符串中抓取文本,其中 container 是我要从中提取文本的元素。
通常,我要提取的文本位于<p> 标记中。所以以下面的 HTML 为例:
<div id="container">
<p>This is the first sentence.</p>
<p>This is the second sentence.</p>
</div>
使用
var container = document.getElementById("container");
var text = container.innerText || container.textContent; // the text I want
将返回This is the first sentence.This is the second sentence.,第一个句点和第二个句子的开头之间没有空格。
我的总体目标是使用 Stanford CoreNLP 解析文本,但它的解析器无法检测到这些是 2 个句子,因为它们没有被空格分隔。有没有更好的方法从 HTML 中提取文本,使句子用空格字符分隔?
我正在解析的 HTML 将在 <p> 标记中包含我最想要的文本,但 HTML 还可能包含 <img>、<a> 以及嵌入在 <p> 标记之间的其他标记。
【问题讨论】:
-
jQuery 标签有什么用途吗?
标签: javascript jquery html regex html-parsing