0

这是一个非常简单的问题,但很少使用正则表达式,所以我很抱歉。我正在为一些简单的 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>

如何从不替换中获得第二个正则表达式?

提前谢谢

4

1 回答 1

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]更有意义。

于 2013-01-20T21:31:43.017 回答