arnklint Challenges, overcomings, thoughts and inspiration.

Using variables in Ruby Regular Expressions

An easy way to deal with variating Regular Expressions.

Use variables in regular expressions

Pretty basic concept, that I can’t seem to remember whenever I need it.

# Note to self!

re = "string"
"my string" =~ /#{re}/    # => 3

# So you can do:

a = 'dog'
b = 'hates cats'
str = "my dog hates cats"
str.match(/#{a} #{b}/)    # => true