user3784251 user3784251 - 1 month ago 5x
Ruby Question

How to interpolate a regex into string and use that string as regex to compare in ruby

Consider i have a strings

goo = "test check\ncode"

if goo =~ /#{Regexp.quote(foo)}/
puts "success!"

I need to compare with "foo" regex. How can i write this regex?

Kindly help me in to find this!


I assume that foo contains a string, which represents the regexp. If so, you can initialize Regexp object from that string and perform your matching as follows:

foo = 'test.*check.*code'

goo =~, Regexp::MULTILINE)