mirror of
https://github.com/tiennm99/hugoDocs.git
synced 2026-10-14 08:18:30 +00:00
Update the findRE and replaceRE functions (#1881)
Co-authored-by: anakinsleftleg <77084568+anakinsleftleg@users.noreply.github.com> Closes #1793
This commit is contained in:
1 parent
28757ec73b
commit
1b25ca34fa
2 files changed
+53
-44
No files matched your search
@@ -1,44 +1,40 @@
|
|||||||
---
|
---
|
||||||
title: findRE
|
title: findRE
|
||||||
description: Returns a list of strings that match the regular expression.
|
description: Returns a slice of strings that match the regular expression.
|
||||||
date: 2017-02-01
|
|
||||||
publishdate: 2017-02-01
|
|
||||||
lastmod: 2017-02-01
|
|
||||||
categories: [functions]
|
categories: [functions]
|
||||||
menu:
|
menu:
|
||||||
docs:
|
docs:
|
||||||
parent: "functions"
|
parent: functions
|
||||||
keywords: [regex]
|
keywords: [regex]
|
||||||
signature: ["findRE PATTERN INPUT [LIMIT]"]
|
signature:
|
||||||
workson: []
|
- "findRE PATTERN INPUT [LIMIT]"
|
||||||
hugoversion:
|
- "strings.FindRE PATTERN INPUT [LIMIT]"
|
||||||
relatedfuncs: []
|
relatedfuncs: [replaceRE]
|
||||||
deprecated: false
|
|
||||||
aliases: []
|
aliases: []
|
||||||
---
|
---
|
||||||
|
By default, the `findRE` function finds all matches. You can limit the number of matches with an optional LIMIT paramater.
|
||||||
|
|
||||||
By default all matches will be included. The number of matches can be limited with an optional third 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 example below returns a list of all second level headers (`<h2>`) in the content:
|
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 returns a slice of all second level headings (`h2` elements) within the rendered `.Content`:
|
||||||
{{ findRE "<h2.*?>(.|\n)*?</h2>" .Content }}
|
|
||||||
|
```go-html-template
|
||||||
|
{{ findRE `(?s)<h2.*?>.*?</h2>` .Content }}
|
||||||
```
|
```
|
||||||
|
|
||||||
You can limit the number of matches in the list with a third parameter. The following example shows how to limit the returned value to just one match (or none, if there are no matched substrings):
|
The `s` flag causes `.` to match `\n` as well, allowing us to find an `h2` element that contains newlines.
|
||||||
|
|
||||||
```
|
To limit the number of matches to one:
|
||||||
{{ findRE "<h2.*?>(.|\n)*?</h2>" .Content 1 }}
|
|
||||||
<!-- returns ["<h2 id="#foo">Foo</h2>"] -->
|
```go-html-template
|
||||||
|
{{ findRE `(?s)<h2.*?>.*?</h2>` .Content 1 }}
|
||||||
```
|
```
|
||||||
|
|
||||||
{{% note %}}
|
{{% 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 write and test your regular expression using [regex101.com](https://regex101.com/). Be sure to select the Go flavor before you begin.
|
||||||
|
|
||||||
If you are just learning RegEx, or at least Go's flavor, you can practice pattern matching in the browser at <https://regex101.com/>.
|
|
||||||
{{% /note %}}
|
{{% /note %}}
|
||||||
|
|
||||||
[partials]: /templates/partials/
|
[RE2]: https://github.com/google/re2/wiki/Syntax
|
||||||
[`plainify`]: /functions/plainify/
|
[string literal]: https://go.dev/ref/spec#String_literals
|
||||||
[toc]: /content-management/toc/
|
|
||||||
[`urlize`]: /functions/urlize
|
|
||||||
@@ -1,34 +1,47 @@
|
|||||||
---
|
---
|
||||||
title: replaceRE
|
title: replaceRE
|
||||||
description: Replaces all occurrences of a regular expression with the replacement pattern.
|
description: Returns a string, replacing all occurrences of a regular expression with a replacement pattern.
|
||||||
date: 2017-02-01
|
|
||||||
publishdate: 2017-02-01
|
|
||||||
lastmod: 2020-09-07
|
|
||||||
categories: [functions]
|
categories: [functions]
|
||||||
menu:
|
menu:
|
||||||
docs:
|
docs:
|
||||||
parent: "functions"
|
parent: functions
|
||||||
keywords: [regex]
|
keywords: [regex]
|
||||||
signature: ["strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]", "replaceRE PATTERN REPLACEMENT INPUT [LIMIT]"]
|
signature:
|
||||||
workson: []
|
- "replaceRE PATTERN REPLACEMENT INPUT [LIMIT]"
|
||||||
hugoversion:
|
- "strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]"
|
||||||
relatedfuncs: []
|
relatedfuncs: [findRE]
|
||||||
deprecated: false
|
|
||||||
aliases: []
|
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
|
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.
|
||||||
expression `PATTERN` with the replacement text `REPLACEMENT`.
|
|
||||||
The number of replacements can be limited with an optional `LIMIT` parameter.
|
|
||||||
|
|
||||||
|
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"
|
To limit the number of replacements to one:
|
||||||
{{ replaceRE "a+b" "X" "aabbaabbab" 1 }} → "Xbaabbab"
|
|
||||||
|
```go-html-template
|
||||||
|
{{ $s := "a-b--c---d" }}
|
||||||
|
{{ replaceRE `(-{2,})` "-" $s 1 }} → a-b-c---d
|
||||||
|
```
|
||||||
|
|
||||||
|
You can use `$1`, `$2`, etc. within the replacement string to insert the groups captured within the regular expression:
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ $s := "http://gohugo.io/docs" }}
|
||||||
|
{{ replaceRE "^https?://([^/]+).*" "$1" $s }} → gohugo.io
|
||||||
```
|
```
|
||||||
|
|
||||||
{{% note %}}
|
{{% 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 write and test your regular expression using [regex101.com](https://regex101.com/). Be sure to select the Go flavor before you begin.
|
||||||
|
|
||||||
If you are just learning RegEx, or at least Go's flavor, you can practice pattern matching in the browser at <https://regex101.com/>.
|
|
||||||
{{% /note %}}
|
{{% /note %}}
|
||||||
|
|
||||||
|
[RE2]: https://github.com/google/re2/wiki/Syntax
|
||||||
|
[string literal]: https://go.dev/ref/spec#String_literals
|
||||||
Reference in new issue
Block a user