Data URI in GO
Match base64-encoded data URIs, including the MIME type and the base64 payload.
Try it in the GO tester →Pattern
regexGO
data:[\w\/+\-.]+(?:;[\w=\-]+)*;base64,[A-Za-z0-9+\/=]+ (flags: gi)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)data:[\w\/+\-.]+(?:;[\w=\-]+)*;base64,[A-Za-z0-9+\/=]+`)
input := `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA`
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
Starts with data:, then a MIME type (word chars, slashes, plus, hyphen, dot), optional parameters, then ;base64, and the base64 payload (letters, digits, +, /, =).
Examples
Input
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAMatches
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA