简体   繁体   English

gsub并点击我不想在rails helper中使用的正则表达式

[英]gsub and hitting on a regex which I don't want to in rails helper

This is really simple question but rarely use regex's so I apologize. 这确实是一个简单的问题,但很少使用正则表达式,因此我深表歉意。 I am writing a simple view helper for some simple UBBcodes 我正在为一些简单的UBBcode写一个简单的视图助手

I want to be able to call: 我希望能够致电:

<%=arc_format "[quote]hello  you from me[\quote]" %>

and have it return: 并返回:

<div class='start-quote'>
  hello you from me
</div>

my helper: 我的助手:

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

the output is 输出是

<div class='start-quote'>
 hello you from me
<div class='start-quote'>
</div>

How do I get that second regex from not replacing? 如何从不替换中获得第二个正则表达式?

thx in advance 提前

The backslash is not being carried over. 反斜杠未继续。 Try: 尝试:

> string = "[quote]hello  you from me[\quote]"

> puts string
[quote]hello  you from me[quote]

Should be: 应该:

> string = "[quote]hello  you from me[\\quote]"

> puts string
[quote]hello  you from me[\quote]

Is it supposed to be [\\quote] ? 应该是[\\quote]吗? I would have thought [/quote] makes more sense. 我本以为[/quote]更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM