I take a regular expression as an input from the user and match values against that regex.
But, the problem i have is that the input i receive is a string.
e.g. "/abc|def/i"
And i am not able to convert it to a regex object.
If it try Regexp.new(string)
it escapes all the characters so i get something like /\/abc|def\/i/
I was able to capture the part between the forward slashes using another regex and build a regexp object using it. For the above example, i capture "abc|def" and when i do Regexp.new("abc|def") i get /abc|def/ which is what i wanted but i need a way to also add the regexp options(e.g. 'i' in the above example) in the string to the ruby regexp object.
How can i achieve this ???
Also, there must be a easier way to achieve all of this. Any help would be greatly appreciated.
Regexp.newis only escaping the "delimiters" (/) and modifiers (i). Why not let the user drop the delimiters and enter any modifiers separately? See how they solved it at rubular.com