Last active
May 12, 2019 21:43
-
-
Save miguelmota/acc44cfa8401c870c32b08f1bf0396c9 to your computer and use it in GitHub Desktop.
Golang replace string using regular expression (regex)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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