Go (RE2)

HTML Attribute in GO

Match HTML attributes of the form name="value" or name='value' and capture both parts.

Try it in the GO tester →

Pattern

regexGO
\b([a-zA-Z][a-zA-Z0-9\-]*)=["']([^"']*)["']   (flags: g)

Go (RE2) code

goGo
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.

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

Same pattern, other engines

← Back to HTML Attribute overview (all engines)