Skip to content

Instantly share code, notes, and snippets.

@rascoop
Last active October 18, 2019 12:57
Show Gist options
  • Save rascoop/80ed29634be40361c08d9bff963480e4 to your computer and use it in GitHub Desktop.
Save rascoop/80ed29634be40361c08d9bff963480e4 to your computer and use it in GitHub Desktop.
Extract Classes from html string
var str = 'bla<p class="c1 c2">blabla<button></button><div id="bla" class=" c1 c3 "></div>';
var classes=getHTMLclasses(str);
console.log(classes);
function getHTMLclasses(html) {
// get all unique css classes in html into dict
var classRegexp = /class=['"](.*?)['"]/g;
var dict = [];
var m;
while ((m = classRegexp.exec(html)))
{
var classes=m[1].replace(/\s+/g, ' ').trim();
classes.split(" ").forEach(function(item) {
dict[item]=true;
});
}
// convert dict to arr
var arr=[];
for (var key in dict) arr.push(key);
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment