How to convert string "/russia/i" to regular expression /russia/i.
I know about Regexp.new str and /#{str}/, but this does not work in case of modifier.
If you can trust your input:
str = "/russia/i"
re = eval(str) # => /russia/i
eval is a good way to go. I don't normally like it that much, but it's straight-ahead, very clear and maintainable. As always, the caveats about using eval with a string the user could have supplied applies.I like this solution
str = "/russia/i"
re = Regexp.new *str.split('/').slice(1,2)
# => /russia/i
s = "/russia/i"
Regexp.new(s[1..s.rindex('/') - 1],s[-1]) # => /russia/i
or
s = "/russia/i"
s.split("/") # => ["", "russia", "i"]
Regexp.new(s.split("/")[1],1) # => /russia/i
or
s = "/russia/i"
s.split("/") # => ["", "russia", "i"]
Regexp.new(*s.split("/")[1..-1]) # => /russia/i
i though?