Last active
October 18, 2019 12:57
-
-
Save rascoop/80ed29634be40361c08d9bff963480e4 to your computer and use it in GitHub Desktop.
Extract Classes from html string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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