HTMLParser2 Extrait du texte de HTML

const htmlparser2 = require('htmlparser2');

const getText = html => {
    const handler = new htmlparser2.DomHandler();
    const parser = new htmlparser2.Parser(handler);

    parser.write(html);
    parser.end();

    return htmlparser2.DomUtils.textContent(handler.root.childNodes);  // or from handler.dom
};


// Example usage:

const html = '<div><p>This is example text 1</p><br /><p>This is example text 2</p></div>';
const text = getText(html);

console.log(text);

Output:

This is example text 1

This is example text 2
Kamran Taghaddos