Skip to content

Instantly share code, notes, and snippets.

@fiatjaf
Created September 24, 2023 13:10
Show Gist options
  • Save fiatjaf/13c73afad4e3132a660effa6a038e40f to your computer and use it in GitHub Desktop.
Save fiatjaf/13c73afad4e3132a660effa6a038e40f to your computer and use it in GitHub Desktop.
golang snippet to remove script tags from a file with crude regex
package main
import (
"os"
"regexp"
"strings"
)
func main() {
for i := 0; i < len(os.Args); i++ {
path := os.Args[i]
scriptRe := regexp.MustCompile(`(?s)<script.*<\/script>`)
endScriptRe := regexp.MustCompile(`</script>`)
file, _ := os.ReadFile(path)
text := string(file)
result := scriptRe.ReplaceAllStringFunc(text, func(s string) string {
index := endScriptRe.FindStringIndex(s)
return strings.TrimSpace(s[index[1]:])
})
os.WriteFile(path, []byte(result), 0644)
}
}

call it with:

go run remove-script-tag.go **.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment