I'm using the following regex in my JavaScript code to parse CSS (style tags and their contents) from a larger string of HTML code
const regex = /<style[^>]*>([^>]*?)<\/style>/g
This works fine, unless the CSS code contained within the style tags includes a CSS child combinator selector (a CSS selector like div > a
for example). I imagine this has something to do with the fact that this particular selector uses >
which is also syntax used to create the actual <style>
tags in HTML, but I don't understand regex well enough to know if there's a way around this?
const str1 = '<style> div { color: red; } a { color: green; } </style>hello<div></div><div><a>hello</a></div>'
const str2 = '<style> div { color: red; } div > a { color: green; } </style>hello<div></div><div><a>hello</a></div>'
const regex = /<style[^>]*>([^>]*?)<\/style>/g
const matches1 = str1.match(regex) // returns a match
const matches2 = str2.match(regex) // does NOT return a match
here's a fiddle
is there a way to modify the regex so that it also works when the CSS code contains a >
?
UPDATE
A clarification based on discussion in the comments
In my particular case, I'm approaching this challenge via regex (rather than say parsing a DOM via document.querySelectorAll('style')
) because the code needs to be able to run in different contexts (the JS runtime is found in various places these days, from browsers, to node to the Adobe suite) and so I was looking for a context agnostic solution
At the moment it seems @Maxt8r solution of changing the content expression to [\S\s]*?
seems to have worked