strings.FindRESubmatch
返回正则表达式全部连续匹配构成的切片,其中每个元素也是一个切片,依次保存最左侧匹配的文本及其各子表达式的匹配结果。
默认情况下,findRESubmatch 会找出所有匹配项。可以用可选的 LIMIT 参数限制匹配数量。返回值为 nil 表示没有匹配。
指定正则表达式时,请使用原始的字符串字面量(反引号),而不要使用解释型字符串字面量(双引号),以简化语法:使用解释型字符串字面量时必须转义反斜杠。
Go 的正则表达式包实现的是 RE2 语法。大致来说,RE2 语法是 PCRE 所接受语法的一个子集,并且有若干注意事项。注意,不支持 RE2 的 \C 转义序列。
演示示例
{{ findRESubmatch `a(x*)b` "-ab-" }} → [["ab" ""]]
{{ findRESubmatch `a(x*)b` "-axxb-" }} → [["axxb" "xx"]]
{{ findRESubmatch `a(x*)b` "-ab-axb-" }} → [["ab" ""] ["axb" "x"]]
{{ findRESubmatch `a(x*)b` "-axxb-ab-" }} → [["axxb" "xx"] ["ab" ""]]
{{ findRESubmatch `a(x*)b` "-axxb-ab-" 1 }} → [["axxb" "xx"]]实用示例
这段 Markdown:
- [Example](https://example.org)
- [Hugo](https://gohugo.io)会生成这样的 HTML:
<ul>
<li><a href="https://example.org">Example</a></li>
<li><a href="https://gohugo.io">Hugo</a></li>
</ul>要匹配其中的链接元素,并捕获链接目标与链接文本:
{{ $regex := `<a\s*href="(.+?)">(.+?)</a>` }}
{{ $matches := findRESubmatch $regex .Content }}上面代码中 $matches 的数据结构用 JSON 表示如下:
[
[
"<a href=\"https://example.org\"></a>Example</a>",
"https://example.org",
"Example"
],
[
"<a href=\"https://gohugo.io\">Hugo</a>",
"https://gohugo.io",
"Hugo"
]
]要渲染其中的 href 属性:
{{ range $matches }}
{{ index . 1 }}
{{ end }}结果:
https://example.org
https://gohugo.io