2

In my rails app, I am saving a question as html. For example,

"<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>"

I need to replace all the width attributes with width:auto;

Help me to do the same. Thanks :)-

1
  • 1
    You seem to have some serious issues with some escaping here (&lt; etc). Commented Feb 26, 2013 at 13:25

1 Answer 1

5

Try this, assuming the string is in a variable s:

s.gsub(/width:[^;]*;/, 'width: auto;')
Sign up to request clarification or add additional context in comments.

4 Comments

I think you need to add the global flag in your regex so that it replaces 'all' width attributes.
Ahh, you're right. I didn't notice you used gsub instead of sub. :)
@Chirag64, Ruby doesn't have a "global flag" for regex patterns. Perhaps you're confusing Ruby and Perl or another language. gsub is the implementation of "global flag", done in a method that does search/replace globally in the String, whereas sub only does the first matching pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.