Create a case-insensitive regular expression from a string in Ruby

Ruby regexes can interpolate expressions in the same way that strings do, using the #{} notation. However, you do have to escape any regex special characters. For example:

input_str = "A man + a plan * a canal : Panama!"
/#{Regexp.escape input_str}/i

Leave a Comment