I'm looking for a regex pattern that will look for an attribute within an HTML tag. Specifically, I'd like to find all instances of ...
style=""
... and remove it from the HTML tag that it is contained within. Obviously this would include anything contained with the double quotes as well.
I'm using Classic ASP to do this. I already have a function setup for a different regex pattern that looks for all HTML tags in a string and removes them. It works great. But now I just need another pattern for specifically removing all of the style attributes.
Any help would be greatly appreciated.
Perhaps a simpler expression is
style="[^\"]*"
so everything between the double quotes except a double quote.
In visual studio find and replace, this is what i do to remove style and class attributes:
\s*style|class="[^"]*\n*"
This removes the beginning spaces and style and class attributes. It looks for anything except a double quote in these attributes and then newline(s), in case if it spreads out to new lines, and lastly adds the closing double quote
I think this might do it:
/style="[a-zA-Z0-9:;\.\s\(\)\-\,]*"/gi
You could also put these in capturing groups, if you wanted to replace some parts only
/(style=")([a-zA-Z0-9:;\.\s\(\)\-\,]*)(")/gi
Working Example: http://regexr.com?2up30
I tried Jason Gennaro's regular expression and slightly modified it
/style="[a-zA-Z0-9:;&\."\s\(\)\-\,]*|\\/ig
This regular expression captures some specific cases with "
inside the string for example
<div class="frame" style="font-family: Monaco, Consolas, "Courier New", monospace; font-size: 12px; background-color: rgb(245, 245, 245);">some text</div>
Try this, it will replace style attribute and it's value completely
const regex = /style="(.*?)"/gm;
const str = `<div class="frame" style="font-family: Monaco, Consolas, "Courier New", monospace; font-size: 12px; background-color: rgb(245, 245, 245);">some text</div>`;
const subst = ``;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
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