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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With