I use the following regex to catch all style HTML tags and the inner content:
<style[^>]*>([^<] )?<[\s\/] style>
That works well, it catches everything till the closing style tag, but fails if the tag content contains other tags ( e.g. svg, path ) See this example https://regex101.com/r/kEyFED/1
How do avoid that and catch such content too?
CodePudding user response:
I would suggest
<style[\w="'\s-]*>(.*?)<\/\s*style>
This should match the style tag and put the inner contents of it into the group 1. It matches the least amount of characters between <style>
and </style>
. The escaping of /
depends on the language you use.
UPDATE: I updated my regex to also match the attributes in an element. The elements are not validated in any kind, the stuff is just there to make the style tag match. Still would not match properly, if >
is contained inside an attribute value.
UPDATE 2: end tag now allows for whitespace between </
and style>
.