Here is a sample string that I need to work with
'<img id="1" data-name="test" src="img_01.jpg" />'
My goal is to extract the attribute name and value pairs from the string, create the element, and apply these attributes.
I have come up with code so far, however, I'm struggling with extracting attribute names and values:
createNode = function(a, b) {
let name = null;
let el = null;
if(/<[a-z][\s\S]*>/i.test(a)) {
name = a.match(/(\w+)/i)[1];
el = document.createElement(name);
// get attributes and apply them
return el;
}
}
createNode('<img id="1" data-name="test" src="img_01.jpg" />');