package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(<iframe.*?)(src=)([^ ]*?\".*?>)(.*?)(<\/iframe>)`)
var str = []byte(`Custom Gida Iframe SRC grabbber <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/JOhiWY7XmoY" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`)
var substitution = []byte("$1class="gida-dummy gida-dummy-youtube" data-src=$3$4$5")
var count = 1 // negative counter is equivalent to global case (replace all)
str = re.ReplaceAllStringFunc(str, func(s string) string {
if count == 0 {
return s
}
count -= 1
return re.ReplaceAllString(s, substitution)
})
fmt.Println(string(str))
}
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/