Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active May 12, 2019 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/acc44cfa8401c870c32b08f1bf0396c9 to your computer and use it in GitHub Desktop.
Save miguelmota/acc44cfa8401c870c32b08f1bf0396c9 to your computer and use it in GitHub Desktop.
Golang replace string using regular expression (regex)
package main
import (
"fmt"
"regexp"
"strings"
)
func main() {
re := regexp.MustCompile(`[^a-zA-Z0-9]`)
slug := strings.ToLower(re.ReplaceAllString("hello world", "-"))
fmt.Println(slug) // "hello-world"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment