Skip to content

Instantly share code, notes, and snippets.

@capJavert
Created February 4, 2020 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save capJavert/6ad4b549c2dfe45da770a59f12d43ff1 to your computer and use it in GitHub Desktop.
Save capJavert/6ad4b549c2dfe45da770a59f12d43ff1 to your computer and use it in GitHub Desktop.
Clear HTML markup from string (the Gmail way)
const clearHtml = (data) => {
let html = data
html = html.replace(/<style([\s\S]*?)<\/style>/gi, '');
html = html.replace(/<script([\s\S]*?)<\/script>/gi, '');
html = html.replace(/<\/div>/ig, '\n');
html = html.replace(/<\/li>/ig, '\n');
html = html.replace(/<li>/ig, ' * ');
html = html.replace(/<\/ul>/ig, '\n');
html = html.replace(/<\/p>/ig, '\n');
html = html.replace(/<br\s*[\/]?>/gi, "\n");
html = html.replace(/<[^>]+>/ig, '');
return html.replace(/\n\n+/g, '\n\n')
}
export default clearHtml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment