| 1 |
--- |
| 2 |
title: strings.FindRE |
| 3 |
description: Returns a slice of strings that match the regular expression. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [findRE] |
| 9 |
returnType: '[]string' |
| 10 |
signatures: ['strings.FindRE PATTERN STRING [LIMIT]'] |
| 11 |
aliases: [/functions/findre] |
| 12 |
--- |
| 13 |
By default, `findRE` finds all matches. You can limit the number of matches with an optional LIMIT argument. |
| 14 |
|
| 15 |
{{% include "/_common/functions/regular-expressions.md" %}} |
| 16 |
|
| 17 |
This example returns a slice of all second level headings (`h2` elements) within the rendered `.Content`: |
| 18 |
|
| 19 |
```go-html-template |
| 20 |
{{ findRE `(?s)<h2.*?>.*?</h2>` .Content }} |
| 21 |
``` |
| 22 |
|
| 23 |
The `s` flag causes `.` to match `\n` as well, allowing us to find an `h2` element that contains newlines. |
| 24 |
|
| 25 |
To limit the number of matches to one: |
| 26 |
|
| 27 |
```go-html-template |
| 28 |
{{ findRE `(?s)<h2.*?>.*?</h2>` .Content 1 }} |
| 29 |
``` |
| 30 |
|
| 31 |
> [!NOTE] |
| 32 |
> You can write and test your regular expression using [regex101.com][]. Be sure to select the Go flavor before you begin. |
| 33 |
|
| 34 |
[regex101.com]: https://regex101.com/ |