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