Search code examples
ruby-on-railsregexruby-on-rails-3rubular

Rails Regular Expression


I'm really new to regular expression. I have the following url: http://www.foo.bar.com/hgur_300x300.jpg and http://www.foo.bar.com/hgur_100x100.jpg

How would I use gsub with regular expression in rails to find [300X300.jpg AND 180X180.jpg] and replace it with 500X500?


Solution

  • "http://www.foo.bar.com/hgur_100x100.jpg".gsub(/\d+/, "500")
    

    will replace the two "100" with "500"

    UPDATE:

    "http://www.foo.bar.com/hgur_100x100.jpg".gsub(/\d+x\d+/, "500x500")
    

    will be more precise