Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS: How to remove style tags and their content from an HTML string, using regular expressions?

I need to remove the entire content of style tags from an html string, in multiple occurrences. I can't use a DOM parser for it.

How could i do this, in JavaScript?

like image 522
i.brod Avatar asked Dec 14 '22 15:12

i.brod


1 Answers

For those that land here in 2020 this worked for me.

string.replace(/(<style[\w\W]+style>)/g, "")

As Bergi alluded to in the OP comments thought, this should be regarded as a last resort if there are no better options. RegEx is not the best way to deal with HTML.

like image 170
JD Guzman Avatar answered May 19 '23 00:05

JD Guzman