Skip to content

Instantly share code, notes, and snippets.

@steffensbola
Last active May 6, 2025 20:57
Show Gist options
  • Select an option

  • Save steffensbola/352fe164fc106487a68e616930f3842c to your computer and use it in GitHub Desktop.

Select an option

Save steffensbola/352fe164fc106487a68e616930f3842c to your computer and use it in GitHub Desktop.
Regex based function to strip style from HTML/ including malformatted html
/**
* Strips all style attributes and style tags from HTML using regex
* (Note: This is a simplified approach - regex may have edge cases with complex HTML)
* @param html Partial HTML string to process (can be invalid)
* @returns HTML string with style attributes and tags removed
*/
function stripStylesWithRegex(html: string): string {
// Remove style attributes (handles single and double quotes)
let cleaned = html.replace(/style\s*=\s*(["']).*?\1/gi, '');
// Remove empty style attributes that might be left behind
cleaned = cleaned.replace(/\s+style\s*=\s*(["'])\s*\1/gi, '');
// Remove style tags and their content
cleaned = cleaned.replace(/<style\b[^>]*>[\s\S]*?<\/style\s*>/gi, '');
// Remove any remaining style attributes without quotes
cleaned = cleaned.replace(/style\s*=\s*[^ >]+/gi, '');
return cleaned;
}
// Example usage
const htmlWithStyles = `
<div style="color: red; font-size: 20px;">
<p style='background: blue;'>Hello</p>
<style>p { margin: 10px; }</style>
<span style=invalid>World</span>
<div style='multiline
style'>
Content
</div>
</div>
`;
const cleanHtml = stripStylesWithRegex(htmlWithStyles);
console.log(cleanHtml);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment