I just stumbled upon the following which I can\'t quite get my head around. When you create a new Regexp in Ruby like so:
Regexp.new(\'http://www.example.com\')
You can do:
Regexp.escape('http://www.example.com')
That will return the fully escaped string
Alternatively, if you just need to check whether a given text contains the specified url you can use String#include?
, and drop the regex altogether. Bearing in mind that Ruby will just do simple string comparisons, this should also be faster.
# before
if text =~ Regexp.new(Regexp.escape('http://www.example.com'))
...
end
# after
if text.include?('http://www.example.com')
...
end