HTML Attribute
Match HTML attributes of the form name="value" or name='value' and capture both parts.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
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).
Python (re) code
import re
pattern = re.compile(r"\\b([a-zA-Z][a-zA-Z0-9\\-]*)=[\"']([^\"']*)[\"']")
input_text = "<a href=\"https://x.com\">"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`\b([a-zA-Z][a-zA-Z0-9\-]*)=["']([^"']*)["']`)
input := `<a href="https://x.com">`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
\b([a-zA-Z][a-zA-Z0-9\-]*)=["']([^"']*)["'] (flags: g)Raw source: \b([a-zA-Z][a-zA-Z0-9\-]*)=["']([^"']*)["']
How it works
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
—Common use cases
- •HTML template parsing
- •Attribute extraction for SEO audits
- •Accessibility linting (alt, aria-*)
- •Static analysis of template files
Related patterns
HTML Tag Matcher
WebMatch paired HTML tags and capture the tag name and inner content using a back-reference.
CSS Class from `class=""` Attribute
WebExtract the value of a `class="..."` attribute from raw HTML, handling double or single quotes.
HTML Entity
WebMatch HTML entities in named (`&`), numeric (`{`), or hex (`💩`) form.
Cookie Header Value
WebParse name=value pairs from an HTTP `Cookie:` header value.