Debian Codegraph @

docs/content/en/functions/strings/ReplaceRE.md

raw
1 ---
2 title: strings.ReplaceRE
3 description: Returns a copy of INPUT, replacing all occurrences of a regular expression with a replacement pattern.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [replaceRE]
9 returnType: string
10 signatures: ['strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]']
11 aliases: [/functions/replacere]
12 ---
13
14 {{% include "/_common/functions/regular-expressions.md" %}}
15
16 ```go-html-template
17 {{ $s := "a-b--c---d" }}
18 {{ replaceRE `(-{2,})` "-" $s }} → a-b-c-d
19 ```
20
21 Limit the number of replacements using the LIMIT argument:
22
23 ```go-html-template
24 {{ $s := "a-b--c---d" }}
25 {{ replaceRE `(-{2,})` "-" $s 1 }} → a-b-c---d
26 ```
27
28 Use `$1`, `$2`, etc. within the replacement string to insert the content of each capturing group within the regular expression:
29
30 ```go-html-template
31 {{ $s := "http://gohugo.io/docs" }}
32 {{ replaceRE "^https?://([^/]+).*" "$1" $s }} → gohugo.io
33 ```
34
35 > [!NOTE]
36 > You can write and test your regular expression using [regex101.com][]. Be sure to select the Go flavor before you begin.
37
38 [regex101.com]: https://regex101.com/