Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove style from HTML Tags using Regex C#

Tags:

html

c#

I want to remove style from HTML Tags using C#. It should return only HTML Simple Tags.

For i.e. if String = <p style="margin: 15px 0px; padding: 0px; border: 0px; outline: 0px;">Hello</p> Then it should return String = <p>Hello</p>

Like that for all HTML Tags, <strong></string>, <b></b> etc. etc.

Please help me for this.

like image 544
CSAT Avatar asked Feb 12 '23 06:02

CSAT


1 Answers

First, as others suggest, an approach using a proper HTML parser is much better. Either use HtmlAgilityPack or CsQuery.

If you really want a regex solution, here it is:

Replace this pattern: (<.+?)\s+style\s*=\s*(["']).*?\2(.*?>)
With: $1$3

Demo: http://regex101.com/r/qJ1vM1/1


To remove multiple attributes, since you're using .NET, this should work:

Replace (?<=<[^<>]+)\s+(?:style|class)\s*=\s*(["']).*?\1
With an empty string

like image 170
Lucas Trzesniewski Avatar answered Feb 15 '23 11:02

Lucas Trzesniewski