Created
February 4, 2020 11:15
-
-
Save capJavert/6ad4b549c2dfe45da770a59f12d43ff1 to your computer and use it in GitHub Desktop.
Clear HTML markup from string (the Gmail way)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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