Skip to content

Instantly share code, notes, and snippets.

@ALEXOTANO
Last active August 14, 2019 20:25
Show Gist options
  • Save ALEXOTANO/172fb482d42853e0b3e3b92d1a434f4c to your computer and use it in GitHub Desktop.
Save ALEXOTANO/172fb482d42853e0b3e3b92d1a434f4c to your computer and use it in GitHub Desktop.
Convert HTML to plain text, strip text from HTML string
Convert HTML to plain text, strip text from HTML string

Some of the regex are for specific use, add as many as you want for exceptcions, but any generic HTML string will be parsed perfectly.

str=str.replace(/{\s*\/*.+?}/ig, "");
str=str.replace(/<\s*br\/*>/gi, "\n");
str=str.replace(/<\s*a.*href="(.*?)".*>(.*?)<\/a>/gi, " $2 (Link->$1) ");
str=str.replace(/<\s*\/*.+?>/ig, "\n");
str=str.replace(/ {2,}/gi, " ");
str=str.replace(/\n+\s*/gi, "\n\n");

/*exceptions for styles and stuff*/
str=str.replace(/@media only screen and \(max-width: 600px\)/ig, "");
str=str.replace(/div\[class=\"column\"\]/ig, "");
str=str.replace(/table\.social/ig, "");
str=str.replace(/{\s*\/*.+?}/ig, "");
str=str.replace('\n\n \r\n\n \r\n\n  {\r\n\nwidth:auto!important;\r\n\n}\r\n\n}\r\n\n', "");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment