summaryrefslogtreecommitdiffstats
path: root/docs/content/en/functions/replacere.md
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-11-17 16:16:19 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-11-17 16:16:19 +0100
commitf04cc581e195bb13803c9e18ca3a3865219e17d4 (patch)
tree04a8a239f75fd81c8ac7a792d8fb96f401bfa651 /docs/content/en/functions/replacere.md
parentcdd83bf3c885c16dc7976ed3697e600029f82bab (diff)
parent00c4484c7092181729f6f470805bc7d72e8ad17b (diff)
Diffstat (limited to 'docs/content/en/functions/replacere.md')
-rw-r--r--docs/content/en/functions/replacere.md51
1 files changed, 32 insertions, 19 deletions
diff --git a/docs/content/en/functions/replacere.md b/docs/content/en/functions/replacere.md
index a2149b6d3..f1e34ba65 100644
--- a/docs/content/en/functions/replacere.md
+++ b/docs/content/en/functions/replacere.md
@@ -1,34 +1,47 @@
---
title: replaceRE
-description: Replaces all occurrences of a regular expression with the replacement pattern.
-date: 2017-02-01
-publishdate: 2017-02-01
-lastmod: 2020-09-07
+description: Returns a string, replacing all occurrences of a regular expression with a replacement pattern.
categories: [functions]
menu:
docs:
- parent: "functions"
+ parent: functions
keywords: [regex]
-signature: ["strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]", "replaceRE PATTERN REPLACEMENT INPUT [LIMIT]"]
-workson: []
-hugoversion:
-relatedfuncs: []
-deprecated: false
+signature:
+ - "replaceRE PATTERN REPLACEMENT INPUT [LIMIT]"
+ - "strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]"
+relatedfuncs: [findRE]
aliases: []
---
+By default, the `replaceRE` function replaces all matches. You can limit the number of matches with an optional LIMIT paramater.
-`strings.ReplaceRE` returns a copy of `INPUT`, replacing all matches of the regular
-expression `PATTERN` with the replacement text `REPLACEMENT`.
-The number of replacements can be limited with an optional `LIMIT` parameter.
+When specifying the regular expression, use a raw [string literal] (backticks) instead of an interpreted string literal (double quotes) to simplify the syntax. With an interpreted string literal you must escape backslashes.
+The syntax of the regular expression is the same general syntax used by Perl, Python, and other languages. More precisely, it is the syntax accepted by [RE2] except for `\C`.
+
+This example replaces two or more consecutive hyphens with a single hyphen:
+
+```go-html-template
+{{ $s := "a-b--c---d" }}
+{{ replaceRE `(-{2,})` "-" $s }} → a-b-c-d
```
-{{ replaceRE "^https?://([^/]+).*" "$1" "http://gohugo.io/docs" }}` → "gohugo.io"
-{{ "http://gohugo.io/docs" | replaceRE "^https?://([^/]+).*" "$1" }}` → "gohugo.io"
-{{ replaceRE "a+b" "X" "aabbaabbab" 1 }} → "Xbaabbab"
+
+To limit the number of replacements to one:
+
+```go-html-template
+{{ $s := "a-b--c---d" }}
+{{ replaceRE `(-{2,})` "-" $s 1 }} → a-b-c---d
```
-{{% note %}}
-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).
+You can use `$1`, `$2`, etc. within the replacement string to insert the groups captured within the regular expression:
-If you are just learning RegEx, or at least Go's flavor, you can practice pattern matching in the browser at <https://regex101.com/>.
+```go-html-template
+{{ $s := "http://gohugo.io/docs" }}
+{{ replaceRE "^https?://([^/]+).*" "$1" $s }} → gohugo.io
+```
+
+{{% note %}}
+You can write and test your regular expression using [regex101.com](https://regex101.com/). Be sure to select the Go flavor before you begin.
{{% /note %}}
+
+[RE2]: https://github.com/google/re2/wiki/Syntax
+[string literal]: https://go.dev/ref/spec#String_literals