Remove Style From Html Tags Using Regex C#
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
Solution 2:
As others said, You can use HTML Agility pack, which has this nice tool: HTML Agility Pack test which shows you what you're doing.
Other than that, it's regex, which is not recommended with HTML usually, or simply running on your code with a loop on all chars. If it starts with <
read until whitespace, and then remove all the chars up until >
. That should take care of most basic cases, but you'll have to test it.
Here's a little snippet that will do it:
voidMain(){
// your input
String input = @"<p style=""margin: 15px 0px; padding: 0px; border: 0px; outline: 0px;"">Hello</p>";
// temp variables
StringBuilder sb = newStringBuilder();
bool inside = false;
booldelete = false;
// analyze stringfor (int i = 0; i < input.Length; i++)
{
// Special case, start bracketif (input[i].Equals('<')) {
inside = true;
delete = false;
}
// special case, close bracketelseif (input[i].Equals('>')) {
inside = false;
delete = false;
}
// other letterselseif (inside) {
// Once you have a space, ignore the rest until closing bracketif (input[i].Equals(' '))
delete = true;
}
// add if neededif (!delete)
sb.Append(input[i]);
}
var result = sb.ToString(); // -> holds: "<p>Hello</p>"
}
Solution 3:
I usually use the below code to remove inline styles, class, images and comments from an Outlook message prior to saving it into database:
desc = Regex.Replace(desc, "(<style.+?</style>)|(<script.+?</script>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);desc = Regex.Replace(desc, "(<img.+?>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);desc = Regex.Replace(desc, "(<o:.+?</o:.+?>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);desc = Regex.Replace(desc, "<!--.+?-->", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);desc = Regex.Replace(desc, "class=.+?>", ">", RegexOptions.IgnoreCase | RegexOptions.Singleline);desc = Regex.Replace(desc, "class=.+?\s", " ", RegexOptions.IgnoreCase | RegexOptions.Singleline);
Solution 4:
All the answers are fine but it can also be done simply by using this method: "Your HTML String".replace("style", "data-tags"); You can also replace "class" the same way.
Solution 5:
source = Regex.Replace(source, "(<style.+?</style>)|(<script.+?</script>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);source = Regex.Replace(source, "(<img.+?>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);source = Regex.Replace(source, "(<o:.+?</o:.+?>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);source = Regex.Replace(source, "<!--.+?-->", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);source = Regex.Replace(source, "class=.+?>", ">", RegexOptions.IgnoreCase | RegexOptions.Singleline);source = Regex.Replace(source.Replace(System.Environment.NewLine, "<br/>"), "<[^(a|img|b|i|u|ul|ol|li)][^>]*>", " ");
Post a Comment for "Remove Style From Html Tags Using Regex C#"