替换 rails 中两个标记之间的某个字符串?
Replace a certain string between two markers in rails?
我想在插入数据库(我正在处理文本区域)之前将代码标记(在我的例子中是```)之间的 "br" 标记替换为 "br" 标记,示例:
```
echo "Hello";
echo "World!";
```
first line.
second line.
会变成:
```<br/>echo "Hello";<br/>echo "World!";<br/>```
first line .
second line .
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.
我想在插入数据库(我正在处理文本区域)之前将代码标记(在我的例子中是```)之间的 "br" 标记替换为 "br" 标记,示例:
```
echo "Hello";
echo "World!";
```
first line.
second line.
会变成:
```<br/>echo "Hello";<br/>echo "World!";<br/>```
first line .
second line .
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.