37

I have a string let's say http://someUrul.com/someController/SOmeAction?SomeQS=http://someOtherUrl

and I want to replace the first http with https, but not the second, so I end up with https://someUrul.com/someController/SOmeAction?SomeQS=http://someOtherUrl

How can I accomplish this with a simple gsub? The following replaces both.

request.url.gsub(/http:/, "https:")
ar3
  • 3,883
  • 3
  • 21
  • 22

1 Answers1

71

Use sub, not gsub. gsub is global, sub isn't.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
HRÓÐÓLFR
  • 5,842
  • 5
  • 32
  • 35