2013-02-26 104 views
2

在我的Rails应用程序中,我将问题保存为html。例如,用另一个字符串替换子串rails

"<p>&lt;pre dir=\"ltr\" style=\"background-color: #ede7db; color: 
#000000; margin: 0px; padding: 6px; border: 1px inset; width: 640px; height: 194px; text-align: left; overflow: auto; background-position: initial initial; background-repeat: initial initial;\" class=\"alt2\"&gt;void myFunc (int x) <br />{ <br /> if (x &amp;gt; 0)<br /> myFunc(--x); <br /> printf(\"%d, \", x); <br />} <br />int main() <br />{ <br /> myFunc(5); <br /> return 0; <br />}&lt;/pre&gt;</p>" 

我需要更换与width:auto;

帮我做同样的全宽度属性。感谢:) -

+1

你似乎有一些严重的问题,一些逃离这里(<等)。 – harm 2013-02-26 13:25:33

回答

5

试试这个,假设这个字符串是一个变量s

s.gsub(/width:[^;]*;/, 'width: auto;') 
+0

我认为你需要在你的正则表达式中添加全局标志,以便它取代'全部'宽度属性。 – 2013-02-26 13:30:10

+3

gsub默认为这样:http://www.ruby-doc.org/core-2.0/String.html#method-i-gsub – alexsanford1 2013-02-26 13:31:19

+0

啊,你说得对。我没有注意到你使用了gsub而不是sub。 :) – 2013-02-26 13:34:07

相关问题