繁体   English   中英

如何在不影响使用 Nodejs 或 Javascript 的 HTML 标签的情况下从 HTML 中获取 100 到 200 个单词?

[英]How can I get 100 to 200 words from HTML without affecting HTML tags using Nodejs or Javascript?

我从我的数据库中获取数据并通过邮件发送,数据带有 HTML 标签。 现在的问题是我只想在邮件中显示 100 个字母。 但是当我删除一些单词时,HTML 标签也会删除,它会破坏整个输出。 请指导我如何实现这一目标。

来自数据库的示例输出

<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="snip">We are looking for physically fit individuals to fill general labour requirements by partnering with a truck driver. Your help is needed to load/unload the truck at the various job sites.
<b>Skills required: </b>
- comfortable with physical exertion and lifting minimum of 50lbs
- works well on a team but trusted to work independently
- reliable, self-motivated and committed to high standards of quality
- able to read and understand work instructions
<b>Specific requirements: </b>
- in good physical condition
- must have own safety footwear
- reliable transportation to ensure punctual and consistent attendance
If you meet the qualifications listed above, submit your resume in MS Word format via the link below.
<i>Previously employed with The Staffing Connection? Please contact our office to confirm your continued availability for these upcoming positions.</i></td>
</tr>
</tbody>
</table>

必需的

<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="snip">We are looking for physically fit individuals to fill general labour requirements by partnering with a truck driver. Your help is needed to load/unload the truck at the various job sites.
<b>Skills required: </b>
- comfortable with physical exertion and lifting minimum of 50lbs
- works well on a team but trusted to work independently
- reliable, self-motivated and committed to high standards of quality
- able to read and understand work instructions
<b></b>
<i></i></td>
</tr>
</tbody>
</table>

我使用类似'Hiya how are you'.substring(0,8);

你的猫使用cheerio libary作为它的:

const cheerio = require('cheerio');

const input = `
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td class="snip">We are looking for physically fit individuals to fill general labour requirements by partnering with a truck driver. Your help is needed to load/unload the truck at the various job sites.
<b>Skills required: </b>
- comfortable with physical exertion and lifting minimum of 50lbs
- works well on a team but trusted to work independently
- reliable, self-motivated and committed to high standards of quality
- able to read and understand work instructions
<b>Specific requirements: </b>
- in good physical condition
- must have own safety footwear
- reliable transportation to ensure punctual and consistent attendance
If you meet the qualifications listed above, submit your resume in MS Word format via the link below.
<i>Previously employed with The Staffing Connection? Please contact our office to confirm your continued availability for these upcoming positions.</i></td>
</tr>
</tbody>
</table>
`;

const result = cheerio.load(input.substring(0, 200), { xmlMode: true });

console.log(result.html());

示例: https : //stackblitz.com/edit/js-wr4fez?file=index.js

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM