我想替换" \ n"进入" br"代码标记之间的标记(在我的情况下是```)在插入DB之前(我正在处理textarea),例如:
```
echo "Hello";
echo "World!";
```
first line.
second line.
会变成:
```<br/>echo "Hello";<br/>echo "World!";<br/>```
first line .
second line .
答案 0 :(得分:0)
str = %q{
```
echo "Hello";
echo "World!";
```
first line.
second line.
}
pattern = %r{
`{3}
.*?
`{3}
}xm
str.gsub!(pattern) do |match|
match.gsub!("\n", '<br/>')
end
puts str
--output:--
```<br/>echo "Hello";<br/>echo "World!";<br/>```
first line.
second line.