AI Summary 11.2515.7 摘要 2026-06-19
Remove all duplicate whitespace

golang regexp whitespace

golang regexp whitespace

\s+ is a regular expression:
the character class \s matches a space, tab, new line, carriage return or form feed,
and + says “one or more of those”.
In other words, the code will replace each whitespace substring with a single space character.

space := regexp.MustCompile(`\s+`)
s := space.ReplaceAllString("Hello  \t \n world!", " ")
fmt.Printf("%q", s) // "Hello world!"