Is it possible to do a case-insensitive `gsub`?
You can pass Regexp
instead of String
, with i
option that indicates that this regexp is case insensitive:
def add_more_ruby(string)
string.gsub(/bad/i, 'good')
end
note that it will substitute not only 'bad'
and 'BAD'
, but also, for example, 'bAd'
. If you want to substitute only all-uppercase or all-lowercase, you can do:
string.gsub(/bad|BAD/, 'good')