2017-02-26 19:27:05 -06:00

1.9 KiB

title linktitle description godocref date publishdate lastmod categories tags signature workson hugoversion relatedfuncs deprecated aliases
findre findRE Returns a list of strings that match the regular expression. 2017-02-01 2017-02-01 2017-02-01
functions
regex
false

Returns a list of strings that match the regular expression. By default all matches will be included. The number of matches can be limitted with an optional third parameter.

The example below returns a list of all second level headers (<h2>) in the content:

{{ findRE "<h2.*?>(.|\n)*?</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):

{{ findRE "<h2.*?>(.|\n)*?</h2>" .Content 1 }}
    <!-- returns ["<h2 id="#foo">Foo</h2>"] -->

findRE Example: Building a Table of Contents

findRE allows us to build an automatically generated table of contents that could be used for a simple scrollspy if you don't want to use Hugo's native .TableOfContents feature. The following shows how this could be done in a partial template:

{{% code file="layouts/partials/toc.html" download="toc.html" %}}

{{ $headers := findRE "<h2.*?>(.|\n)*?</h2>" .Content }}
{{ if ge (len $headers) 1 }}
    <ul>
    {{ range $headers }}
        <li>
            <a href="#{{ . | plainify | urlize }}">
                {{ . | plainify }}
            </a>
        </li>
    {{ end }}
    </ul>
{{ end }}

{{% /code %}}

The preceding snippet tries to find all second-level headers and generate a list where at least one header is found. plainify strips the HTML and urlize converts the header into a valid URL.