Regex remove HTML attribute from any HTML tag (style = "")?

I am looking for a regex pattern that will look for an attribute in an HTML tag. In particular, I would like to find all instances ...

style="" 

... and remove it from the HTML tag in which it is contained. Obviously, this will include everything contained in double quotes.

I am using Classic ASP for this. I already have a function for another regex pattern that searches for all HTML tags in a string and removes them. It works great. But now I just need another template to specifically remove all the style attributes.

Any help would be greatly appreciated.

+4
source share
5 answers

I think this can do it:

/style="[a-zA-Z0-9:;\.\s\(\)\-\,]*"/gi

You can also put them in capture groups if you want to replace only some parts.

/(style=")([a-zA-Z0-9:;\.\s\(\)\-\,]*)(")/gi

Working example: http://regexr.com?2up30

+13
source

Perhaps a simpler expression

  style="[^\"]*" 

so everything between double quotes except double quotes.

+21
source

This works with perl. You may need to modify the regex to comply with ASP rules a bit, but it should work for any tag.

 $file=~ s/(<\s*[az][a-z0-9]*.*\s)(style\s*=\s*".*?")([^<>]*>)/$1 $3/sig; 

Where is the line html file.

Also this is in .net C #

  string resultString = null; string subjectString = "<html style=\"something\"> "; resultString = Regex.Replace(subjectString, @"(<\s*[az][a-z0-9]*.*\s)(style\s*=\s*"".*?"")([^<>]*>)", "$1 $3", RegexOptions.Singleline | RegexOptions.IgnoreCase); 

Result: <html >

0
source

This expression works for me:

 style=".+"/ig 
0
source

I tried the regular expression of Jason Gennaro and changed it a little

/style="[a-zA-Z0-9:;&\."\s\(\)\-\,]*|\\/ig

This regex captures some specific cases with " inside a string, e.g.

  <div class="frame" style="font-family: Monaco, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; background-color: rgb(245, 245, 245);">some text</div> 
0
source

All Articles