Javascript 如何获取文本

Javascript 如何获取文本,javascript,cheerio,Javascript,Cheerio,我有一些html需要整理 <div class="content"> <strong> This is first content </strong> This is second content <br /> <small> <p>Something</p> </small> </div> 这是第一个内容这是第二个内容 某物 如何获取这是第二个内容与che

我有一些html需要整理

<div class="content">
  <strong> This is first content </strong> This is second content
  <br />
  <small>
    <p>Something</p>
  </small>
</div>

这是第一个内容这是第二个内容

某物


如何获取这是第二个内容cheerio

我认为您可以使用正则表达式获取第二个内容

    const cheerio = require('cheerio');
    const $ = cheerio.load(`<div class="content">
    <strong> This is first content </strong> This is second content

    <br />
    <small>
    <p> Something </p>
    </small>
    </div>
    `);
    console.log($('div').html().replace(/\n/g, '').match(/<\/strong>(.*)<br>/)[1])
const cheerio=require('cheerio');
const$=cheerio.load(`
这是第一个内容这是第二个内容

某物

`); console.log($('div').html().replace(/\n/g',).match(/(.*)
/)[1])
也许这会有帮助:

<div class="content">
  <strong> This is first content </strong> <span class="toBeSelected">This is second content</span>
  <br />
  <small>
    <p>Something</p>
  </small>
</div>

理想情况下,您应该将“这是第二个内容”放在span或其他适当指定的内容中,以获取其内容

这样做:

<div class="content">
 <strong>This is first content</strong><span>This is second content</span>
<br>
  <small>
    <p>Something</p>
  </small>
</div>

工作演示:为您准备。

使用
节点类型
属性,即使您在
标记之前有文本,它也可以解决您的问题

<div class="content">
  Before first content
  <strong> This is first content </strong> This is second content
  <br />
  <small>
    <p>Something</p>
  </small>
</div>

在第一个内容之前
这是第一个内容这是第二个内容

某物

那可能是

var cheerio = require("cheerio")
const $ = cheerio.load('<div class="content">Before first content<strong> This is first content </strong> This is second content<br /><small><p>Something</p></small></div>');

var $outer = $("div.content").contents().filter(function() {
    return this.nodeType === 3;
});

console.log($outer.text()); //"Before first content This is second content"

$outer.each(function() {
    console.log($(this).text());
});
//"Before first content"
//" This is second content"
var cheerio=require(“cheerio”)
const$=cheerio.load('在第一个内容之前这是第一个内容这是第二个内容
某物

'); var$outer=$(“div.content”).contents().filter(函数()){ 返回this.nodeType==3; }); log($outer.text())//“在第一个内容之前,这是第二个内容” $outer.each(函数(){ console.log($(this.text()); }); //“在第一个内容之前” //“这是第二个内容”

选中它

您不能直接选择文本节点。我通常会这样做:

$('.content strong')[0].nextSibling.data

这与OP提供的内容不匹配。我不知道如何使用cheerio进行此操作,但请仅使用javascript查看此示例,它可以让您了解如何使用cheerio进行此操作想法是获取容器的所有子节点,包括文本类型,然后过滤以仅列出文本类型的子节点,然后使用消除空格的修剪方法变换每个节点,并最终过滤包含某些内容的文本。我不熟悉ChereIO,但它似乎与jQuery共享类似的api,如果是这样,那么中的示例将帮助您删除\n、\t和(长空间)。我使用了
.trim()
,但是\n和其他没有基本上删除
。trim()
在这种情况下(开始和结束)效果好吗?您遇到了什么html?
var cheerio = require("cheerio")
const $ = cheerio.load('<div class="content">Before first content<strong> This is first content </strong> This is second content<br /><small><p>Something</p></small></div>');

var $outer = $("div.content").contents().filter(function() {
    return this.nodeType === 3;
});

console.log($outer.text()); //"Before first content This is second content"

$outer.each(function() {
    console.log($(this).text());
});
//"Before first content"
//" This is second content"
$('.content strong')[0].nextSibling.data