diff --git a/layouts/_partials/toc.html b/layouts/_partials/toc.html
index 800f9aab..4fc93565 100644
--- a/layouts/_partials/toc.html
+++ b/layouts/_partials/toc.html
@@ -1,4 +1,4 @@
-{{- $headers := findRE "(.|\n])+?" .Content -}}
+{{- $headers := findRE "(.|\n)+?" .Content -}}
{{- $has_headers := ge (len $headers) 1 -}}
{{- if $has_headers -}}
@@ -36,7 +36,7 @@
{{- /* strip id="" to leave xyz, no way to get regex capturing groups in hugo */ -}}
{{- $cleanedID := replace (replace $id "id=\"" "") "\"" "" }}
- {{- $header := replaceRE "((.|\n])+?)" "$1" $header -}}
+ {{- $header := replaceRE "((.|\n)+?)" "$1" $header | strings.TrimSpace -}}
{{- if ne $i 0 -}}
{{- $prevHeaderLevel := index (findRE "[1-6]" (index $headers (sub $i 1)) 1) 0 -}}