Hugo替换但忽略大小写。

huangapple go评论69阅读模式
英文:

Hugo Replace but Ignore Case-sensitive

问题

使用这段代码,但不区分大小写。我想要替换所有包含Esports、esports、eSports、ESports等单词的部分。

{{ $content := replace $content "Esports" "<a href=\"https://example.com/esports/\">Esports</a>" 1 }}
英文:

With this code but without case sensitive. I want replace all word contain Esports, esports, eSports, ESports etc..

{{ $content := replace $content &quot;Esports&quot;  &quot;&lt;a href=\&quot;https://example.com/esports/\&quot; &gt;Esports&lt;/a&gt;&quot; 1 }}

答案1

得分: 1

你可以使用replaceRE函数进行正则表达式匹配,并使用i标志使其不区分大小写。

{{ $content | replaceRE "(?i:esports)" "<a href=\"https://example.com/esports/\">Esports</a>" }}

你可能需要调整标志或正则表达式。这只是一个概要,我还没有测试过。

英文:

You can use replaceRE which does a regex match and then use the i flag to make it case-insensitive.

{{ $content | replaceRE &quot;(?i:esports)&quot; &quot;&lt;a href=\&quot;https://example.com/esports/\&quot; &gt;Esports&lt;/a&gt;&quot; }}

You may need to tweak the flags or the regex. This is just the gist. I have not tested it.

huangapple
  • 本文由 发表于 2022年8月20日 15:55:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/73424987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定