Skip to content

Instantly share code, notes, and snippets.

@prashant-shahi
Created January 17, 2024 08:58
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 prashant-shahi/219ae1a8ae67ae5d8b9417de6cb0a147 to your computer and use it in GitHub Desktop.
Save prashant-shahi/219ae1a8ae67ae5d8b9417de6cb0a147 to your computer and use it in GitHub Desktop.
kebab casing string for valid URL paths in Golang
package main
import (
"fmt"
"regexp"
"strings"
)
func toKebabCase(s string) string {
// Regular expression to match non-alphanumeric characters
reg := regexp.MustCompile("[^a-zA-Z0-9]+")
// Replace non-alphanumeric characters with hyphen
kebab := reg.ReplaceAllString(s, "-")
// Convert to lowercase
return strings.ToLower(kebab)
}
func main() {
testStrings := []string{"Hello/World", "hello world", "HeLLo%$./@@!^&*()WoRlD", "h3LL0^w0rLd"}
for _, s := range testStrings {
fmt.Println(toKebabCase(s))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment