Ruby experts:
Please advise how I can make a gsub operation NOT case sensitive, so that for example this command:
some_big_string.gsub!(/soMe_sUbString_1/, some_substring_2)
would do a replacement of all occurances of “soMe_sUbString_1” regardless of the case of the target string in some_big_string. Eg, the text “SOME_SUBSTRING_1” in some_big_string WOULD be replaced by “some_substring_2” using the command (or technique) I’m looking for.
Thanks.
-Kurt Euler
some_big_string.gsub!(/soMe_sUbString_1/, some_substring_2)
Just stick an ‘i’ outside your // on the regex:
some_big_string.gsub!(/soMe_sUbString_1/i, some_substring_2)
Chris
http://clabs.org
add an ‘i’ after the expression.
ie:
some_big_string = ‘SOME_substring_1 foo bar’
some_big_string.gsub!(/soMe_sUbString_1/i, ‘xxx’)
puts some_big_string → xxx foo bar
Kurt Euler wrote:
···
Ruby experts:
Please advise how I can make a gsub operation NOT case sensitive, so that for example this command:
some_big_string.gsub!(/soMe_sUbString_1/, some_substring_2)
would do a replacement of all occurances of “soMe_sUbString_1” regardless of the case of the target string in some_big_string. Eg, the text “SOME_SUBSTRING_1” in some_big_string WOULD be replaced by “some_substring_2” using the command (or technique) I’m looking for.
Thanks.
-Kurt Euler