简体   繁体   中英

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. Now problem is I want to display only 100 letters in mail. But when I remove some words HTML tags also remove and it will destroy whole Output. 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);

You cat use cheerio libary for its:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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