Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
所以从这个字符串:
str = 'Hello #{name}, you are now #{age}.'
我想得到
"Hello <%= name %>, you are now <%= age =>"
我试过这个:
str.gsub(/\#{*}/, "<%= \1 %>")
但我这行不通。
要捕获组,您必须用括号括住该部分。
str = 'Hello #{name}, you are now #{age}.' str.gsub(/#\{(.*?)\}/, '<%= \1 %>') # => "Hello <%= name %>, you are now <%= age %>."
而且,你必须逃避{,,}而不是#。( {,}在正则表达式中有特殊含义,while#没有)。
{
}
#