(I am using JavaScript to do it)
If you are certain there is no "
inside the class name of class="abc xyz"
, then you can use
/<(.+?)class=\s*"([^"]*?)"/g
Example:
([...'<h1 class="big blue" id="testing"> some text </h1><div id="foo" class="blue danube page-title"> some text </div><span class=""></span>'
.matchAll(/<(.+?)class=\s*"([^"]*?)"/g)].map(arr => arr[2]))
would give
["big blue", "blue danube page-title", ""]
One bug about non-greedy: it is .+?
and if you have (.+)?
it means match as much as possible and then "optional".
The other concern is you probably want to match class=""
as ""
, so it'd be [^"]*
rather than [^"]+
One issue with your orignail regex is that you match the ending >
, so it has to match to the end even if you say non-greedy. You can see https://regex101.com/r/0weyDs/3 for
<(.+?)class=\s*"(.+?)"
or https://regex101.com/r/0weyDs/4 for the first regex in the answer.