简体   繁体   中英

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

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] ? I would have thought [/quote] makes more sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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