这是一个非常简单的问题,但很少使用正则表达式,所以我道歉。我正在为一些简单的UBBcodes编写一个简单的视图助手
我想要打电话:
<%=arc_format "[quote]hello you from me[\quote]" %>
并让它返回:
<div class='start-quote'>
hello you from me
</div>
我的帮手:
def arc_format str
str=str.gsub(/\[quote\]/,'<div class="start-quote">') # works but adds in second quote; seems to hit off second isntance
str=str.gsub!(/\[\\quote\]/,'</div>')
str.html_safe
end
输出
<div class='start-quote'>
hello you from me
<div class='start-quote'>
</div>
如何从不替换第二个正则表达式?
事先提前答案 0 :(得分:2)
反斜杠未被结转。尝试:
> string = "[quote]hello you from me[\quote]"
> puts string
[quote]hello you from me[quote]
应该是:
> string = "[quote]hello you from me[\\quote]"
> puts string
[quote]hello you from me[\quote]
它应该是[\quote]
吗?我原以为[/quote]
更有意义。