-2

I want to reduce the text so that only displays 30 characters Like <p>Piqued favour stairs it enable exeter as seeing</p> I want to be a text <p>Piqued favour stairs it...</p>

using javascript

("Show a certain number of text characters")

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216

2 Answers2

1

This is may be the most simple answer I can give using vanilla JS. It should work for you. Let me know, if not.

const trimSize = 20;
let text = '<p>Something that is too big and you want to trim, and then renddeer in your HTML page.</p>';

let content = text.substring(3, text.length - 4);

let trimmedText = '<p>' + content.substring(0, trimSize) + '<\p>';
console.log(trimmedText);
Mukul Jain
  • 1,121
  • 11
  • 24
0

This might be the answer to your question:

const trim = (data, len) =>
    data.replace(/(<(\w+)[^>]*?>)(\w.*?)<\/\2>/g,
        match => match.replace(/(?=>).*(?=<\/)/,
            match => match.length > len ? match.substr(0, len) : match
        ))

console.log(trim(`<div class="blog_contents"><h3 id="foo123">Facebook launches the application of a special communication companies</h3></div> `, 20))
Maciej Kozieja
  • 1,812
  • 1
  • 13
  • 32