简体   繁体   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?

I am getting data from my database and send in mail, data comes with HTML tags.我从我的数据库中获取数据并通过邮件发送,数据带有 HTML 标签。 Now problem is I want to display only 100 letters in mail.现在的问题是我只想在邮件中显示 100 个字母。 But when I remove some words HTML tags also remove and it will destroy whole Output.但是当我删除一些单词时,HTML 标签也会删除,它会破坏整个输出。 Please guide me how can I achieve this.请指导我如何实现这一目标。

Sample Output From Database来自数据库的示例输出

<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>

Required必需的

<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>

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

You cat use cheerio libary for its:你的猫使用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());

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

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

相关问题 在JavaScript中,如何在不影响标记的情况下替换HTML页面中的文本? - In JavaScript, how can I replace text in an HTML page without affecting the tags? 在不影响标签的情况下替换html中的所有单词 - Replace all words in html without affecting tags 如何替换包含HTML标签的单词而不丢失HTML标签? - How can I replace words that contain HTML tags without losing the HTML tags? 使用javascript正则表达式获取没有html标签的所有单词 - get all words without html tags with javascript regex 如何在不使用Javascript编码标签本身的情况下HTML编码可能具有html标签的文本 - How can I HTML encode text that may have html tags without encoding the tags themselves in Javascript 如何在保留 HTML 标签(Javascript/NodeJS)的同时清理字符并将其转换为 HTML 实体? - How can I sanitize and convert characters to HTML entities while preserving HTML tags (Javascript/NodeJS)? 如何在不使用regexp的情况下从JavaScript中的字符串中删除HTML标签? - how to remove HTML tags from a string in JavaScript without using regexp? 如何获取这些HTML标签以从javascript呈现 - How do I get these HTML tags to render from javascript 如何使用 Javascript 创建 HTML 标签? - How can I create HTML tags using Javascript? 如何使用 Javascript 在字符串中找到类似 HTML 的标签? - How can I find HTML like tags in a string using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM