HTML Attribute in JS
Match HTML attributes of the form name="value" or name='value' and capture both parts.
Try it in the JS tester →Pattern
regexJS
\b([a-zA-Z][a-zA-Z0-9\-]*)=["']([^"']*)["'] (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("\\b([a-zA-Z][a-zA-Z0-9\\-]*)=[\"']([^\"']*)[\"']", "g");
const input = "<a href=\"https://x.com\">";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
Group 1 captures the attribute name (letters, digits, hyphen). Group 2 captures the quoted value. Matches both double and single quoted forms.
Examples
Input
<a href="https://x.com">Matches
href="https://x.com"
Input
<img alt='logo' src='/a.png'>Matches
alt='logo'src='/a.png'
Input
<div></div>No match
—