I want to convert below html string into recommended whatsapp message format using javascript
let htmlText = '<b>TEST </b> BODY <i><b>WITH </b></i>SAMPLE <strike style="font-weight: bold; font-style: italic;">FORMAT </strike> HERE"';
into below format
*TEST* BODY _*WITH *_ SAMPLE *_~FORMAT~_* HERE
2
Answers
Based on the @trincot solution, added some additional changes
I would advise using a DOM Parser to parse the HTML, and then iterate over the DOM you get from it. This way the resulting text will also have all HTML entities resolved to text, any HTML comments will have been removed, and it will not break when spacing in HTML tags or their attributes is different from expected.
I would also make sure the formatting characters are put adjacent to the word they apply to, so leaving any surrounding white space out of it: