使用JSCSSP
CSS
<textArea id="source"></textArea>
<button id="parse">Parse</button>
Javascript
var source = document.getElementById("source"),
ss,
parser,
sheet,
length1,
index1,
length2,
index2,
myArray,
cssRule,
declaration,
selector,
bg;
document.getElementById("parse").addEventListener("click", function () {
ss = source.value;
parser = new CSSParser();
sheet = parser.parse(ss, false, true);
if (sheet) {
myArray = [];
for (index1 = 0, length1 = sheet.cssRules.length; index1 < length1; index1 += 1) {
cssRule = sheet.cssRules[index1];
selector = cssRule.mSelectorText;
for (index2 = 0, length2 = cssRule.declarations.length; index2 < length2; index2 += 1) {
declaration = cssRule.declarations[index2];
if (declaration.property === "background-image") {
bg = declaration.valueText.match(/url\((\S+)\)/i)[1];
myArray.push({
"selector": selector,
"bg": bg
});
break
}
}
}
console.log(myArray);
}
});
jsfiddle