Slug (Unicode Letters Allowed) in GO
Validate URL slugs allowing Unicode letters (`café-rénové`), as supported by Next.js i18n routing.
Try it in the GO tester →Pattern
regexGO
^[\p{L}\p{N}]+(?:-[\p{L}\p{N}]+)*$ (flags: u)Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[\p{L}\p{N}]+(?:-[\p{L}\p{N}]+)*$`)
input := `hello-world`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
How the pattern works
[\p{L}\p{N}]+ matches one or more Unicode letters or numbers (so `café` and `東京` are valid). (?:-[\p{L}\p{N}]+)* allows additional hyphen-separated segments. The u flag is REQUIRED in JavaScript for \p{} property escapes; Python's stdlib `re` doesn't support \p{} (use \w with Unicode flag, or the `regex` package); Go RE2 has its own \p{...} support.
Examples
Input
hello-worldMatches
hello-world
Input
café-rénovéMatches
café-rénové
Input
trailing-No match
—