Javascript Regex Get Class Name
* I've already searched stack overflow and tried answers for all of the similar questions * I'm writing a node script that: Scans a directory for html files Reads the file conten
Solution 1:
I think this might be the answer:
const data = `<section class="mb-4" id="button-test">
<button type="button" class="btn btn-primary">Primary</button>
<button type="button" class="btn btn-secondary">
Secondary
</button>
<button type="button" class="btn btn-success">
Success
</button>
</section>`const elements = []
const findTag = /<[^\/].*?>/glet element
while(element = findTag.exec(data)){
element = element[0]
const id = (element.match(/id="(.*?)"/i) || [, ""])[1]
const classes = (element.match(/class="(.*?)"/i) || [,""])[1].split(' ')
element = {}
element["id"] = id
element["class"] = classes
elements.push(element)
}
console.log("All elements")
console.log(elements)
// You can now also filter elementsconsole.log("All elements having btn class")
console.log(elements.filter(element => element.class.indexOf("btn") != -1))
Post a Comment for "Javascript Regex Get Class Name"