gpt4 book ai didi

ruby-on-rails - Ruby 正则表达式匹配双 mustache 但不匹配三 mustache

转载 作者:数据小太阳 更新时间:2023-10-29 08:43:58 25 4
gpt4 key购买 nike

我在我的 ruby​​ on rails 应用程序中使用了 mustache gem。给定一个模板,我想用三 mustache 替换模板中的所有双 mustache ,并且当它是三 mustache 时不替换。原来的模板不会有单 mustache ,所以我不想担心单 mustache 。

例如:

temp = "You have just won {{value}} {{{currency}}}!"

应该转换为

temp = "You have just won {{{value}}} {{{currency}}}!"

最佳答案

这可以通过否定前瞻来完成。

\{\{[^}]+\}\}(?!\})

这将匹配由 2 个 mustache 包围但后面没有第三个 mustache 的子字符串。

然后可以用额外的 mustache 包围火柴。

temp = "You have just won {{value}} {{{currency}}}!"
temp.gsub!(/\{\{[^}]+\}\}(?!\})/, '{\&}')
puts temp

关于ruby-on-rails - Ruby 正则表达式匹配双 mustache 但不匹配三 mustache ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37630326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com