replacere.md (1397B)
1 --- 2 title: replaceRE 3 description: Replaces all occurrences of a regular expression with the replacement pattern. 4 date: 2017-02-01 5 publishdate: 2017-02-01 6 lastmod: 2020-09-07 7 categories: [functions] 8 menu: 9 docs: 10 parent: "functions" 11 keywords: [regex] 12 signature: ["strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]", "replaceRE PATTERN REPLACEMENT INPUT [LIMIT]"] 13 workson: [] 14 hugoversion: 15 relatedfuncs: [] 16 deprecated: false 17 aliases: [] 18 --- 19 20 `strings.ReplaceRE` returns a copy of `INPUT`, replacing all matches of the regular 21 expression `PATTERN` with the replacement text `REPLACEMENT`. 22 The number of replacements can be limited with an optional `LIMIT` parameter. 23 24 ``` 25 {{ replaceRE "^https?://([^/]+).*" "$1" "http://gohugo.io/docs" }}` → "gohugo.io" 26 {{ "http://gohugo.io/docs" | replaceRE "^https?://([^/]+).*" "$1" }}` → "gohugo.io" 27 {{ replaceRE "a+b" "X" "aabbaabbab" 1 }} → "Xbaabbab" 28 ``` 29 30 {{% note %}} 31 Hugo uses Go's [Regular Expression package](https://golang.org/pkg/regexp/), which is the same general syntax used by Perl, Python, and other languages but with a few minor differences for those coming from a background in PCRE. For a full syntax listing, see the [GitHub wiki for re2](https://github.com/google/re2/wiki/Syntax). 32 33 If you are just learning RegEx, or at least Go's flavor, you can practice pattern matching in the browser at <https://regex101.com/>. 34 {{% /note %}}