curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68cae5b538ff798bbd4da008.md
Given a string of a valid HTML element, return the attributes of the element using the following criteria:
attribute="value".["attribute1, value1", "attribute2, value2"].extractAttributes('<span class="red"></span>') should return ["class, red"].
assert.deepEqual(extractAttributes('<span class="red"></span>'), ["class, red"]);
extractAttributes('<meta charset="UTF-8" />') should return ["charset, UTF-8"].
assert.deepEqual(extractAttributes('<meta charset="UTF-8" />'), ["charset, UTF-8"]);
extractAttributes("<p>Lorem ipsum dolor sit amet</p>") should return [].
assert.deepEqual(extractAttributes("<p>Lorem ipsum dolor sit amet</p>"), []);
extractAttributes('<input name="email" type="email" required="true" />') should return ["name, email", "type, email", "required, true"].
assert.deepEqual(extractAttributes('<input name="email" type="email" required="true" />'), ["name, email", "type, email", "required, true"]);
extractAttributes('<button id="submit" class="btn btn-primary">Submit</button>') should return ["id, submit", "class, btn btn-primary"].
assert.deepEqual(extractAttributes('<button id="submit" class="btn btn-primary">Submit</button>'), ["id, submit", "class, btn btn-primary"]);
function extractAttributes(element) {
return element;
}
function extractAttributes(element) {
const regex = /(\w[\w-]*)="([^"]*)"/g;
const result = [];
let match;
while ((match = regex.exec(element)) !== null) {
const attr = match[1];
const val = match[2];
result.push(`${attr}, ${val}`);
}
return result;
}