package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)<img\s+[^>]*?src=["\']?([^"\'> ]+)["\']?[^>]*>`)
var str = `<!-- works -->
<div><img class="img-responsive" src="/images/dir%20with%20spaces/file%20with%20spaces.png?ver=1.0" /></div>
<div><img class="img-responsive" src="/images/dir/file-with-accents-áépi.png?ver=1.0" /></div>
<div><img class="img-responsive" src=unquotedimage.jpg /></div>
<div><img class="img-responsive" src=unquotedimagewithdoublequote".jpg /></div>
<div><img class="img-responsive" src=unquotedimagewithsinglequote'.jpg /></div>
<div><img class="img-responsive" src=unquotedimagewithcolon:.jpg /></div>
<!-- not working -->
<div><img class="img-responsive" src="/images/dir with spaces/file with spaces.png?ver=1.0" /></div>
`
for i, match := range re.FindAllString(str, -1) {
fmt.Println(match, "found at index", i)
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Golang, please visit: https://golang.org/pkg/regexp/