hugo/content/en/functions/strings/FindRe.md
Bjørn Erik Pedersen 9b0050e9aa Squashed 'docs/' content from commit 5c085a37b
git-subtree-dir: docs
git-subtree-split: 5c085a37b297bf12f59efeaae591418ec025c10d
2024-01-27 10:48:33 +01:00

1.1 KiB

title description categories keywords action aliases
strings.FindRE Returns a slice of strings that match the regular expression.
aliases related returnType signatures
findRE
functions/strings/FindRESubmatch
functions/strings/Replace
functions/strings/ReplaceRE
[]string
strings.FindRE PATTERN INPUT [LIMIT]
/functions/findre

By default, findRE finds all matches. You can limit the number of matches with an optional LIMIT argument.

{{% include "functions/_common/regular-expressions.md" %}}

This example returns a slice of all second level headings (h2 elements) within the rendered .Content:

{{ findRE `(?s)<h2.*?>.*?</h2>` .Content }}

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 `(?s)<h2.*?>.*?</h2>` .Content 1 }}

{{% note %}} You can write and test your regular expression using regex101.com. Be sure to select the Go flavor before you begin. {{% /note %}}