ar3
ar3

Reputation: 4063

Ruby match first occurrence of string for a gsub replacement

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

Upvotes: 37

Views: 16461

Answers (1)

HRÓÐÓLFR
HRÓÐÓLFR

Reputation: 5982

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

Upvotes: 74

Related Questions