Scenario
Extracting URLs from multiple CSS url()
functional notation.
Given the following css value from project bootstrap:
src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff2') format('woff2'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');
I need an array of URL strings like ["../fonts/glyphicons-halflings-regular.eot?#iefix", "../fonts/glyphicons-halflings-regular.woff2", ...]
.
Solution
Right now I use a while loop and regex matcher to extract URL strings from the parsed declaration via a CSS parser:
var decValue = "url(foo), url(bar)";
// no data URI
var regexp = new RegExp(/url\(([^\(|^\)|^\"|^\']+)\)/g),
matches = [],
match;
while (match = regexp.exec(decValue), match !== null) {
if (match[1].indexOf("data:") === -1) {
matches.push(match[1]);
}
}
// should print ["foo", "bar"]
console.log(matches);
Question
Is there a way to do this without using a while loop but keeping group matching?