strings.ReplaceRE
Returns a copy of INPUT, replacing all occurrences of a regular expression with a replacement pattern.
Syntax
strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]
Returns
string
Alias
replaceRE
在指定正则表达式时,使用原始 [字符串字面量](反引号)而不是解释字符串字面量(双引号)以简化语法。使用解释字符串字面量时,您必须转义反斜杠。
Go 的正则表达式包实现了 [RE2 语法]。RE2 语法是 PCRE 接受的内容的子集(粗略地说),并带有各种 [注意事项]。请注意,不支持 RE2 \C 转义序列。
{{ $s := "a-b--c---d" }}
{{ replaceRE `(-{2,})` "-" $s }} → a-b-c-dLimit the number of replacements using the LIMIT argument:
{{ $s := "a-b--c---d" }}
{{ replaceRE `(-{2,})` "-" $s 1 }} → a-b-c---dUse $1, $2, etc. within the replacement string to insert the content of each capturing group within the regular expression:
{{ $s := "http://gohugo.io/docs" }}
{{ replaceRE "^https?://([^/]+).*" "$1" $s }} → gohugo.ioYou can write and test your regular expression using regex101.com. Be sure to select the Go flavor before you begin.