From a large minified css file I want to extract the style inside specific media queries. Something like:
.some-class{aaa:bbb;ccc:ddd;}@media (min-width: 500px){.some-other-class{aaa:bbb;ccc:ddd;}}.another-class{aaa:bbb;ccc:ddd;}
The following regex selects the media queries, with a capturing group for the style inside the media query:
regex = new RegExp("@media.\\(min-width:\\d{3,4}px\\){(.+?\\})\\}", "g");
The capturing group result is the part I actually want. But when there are more than one hits how do I access multiple capturing group results. When there is only one the result I am after is the second item in the resulting array, when using str.match(regex);
.
Is the only solution using regex.exec(str)[1]
in combination with a while loop? Like:
while(result != null){
result = regex.exec(str)[1];
resultArr.push(result);
}
I'd much rather use match()
...I find that more readable and a more elegant solution, you immediately get an array with you results...although the results I am looking for are only a subset of the results I am getting...
I guess I will have to use the last code snippet I gave,
regex.exec(str)
. Ahh well...