In IRB if I pass a string like "/domain/path" to Regexp.escape it j开发者_运维百科ust returns it the same. I thought that forward slashes are supposed to be escaped with a backslash? Am I missing something here?
Also, the only reason why you would need to escape /
characters is because it is your delimiter for the regexp, if you specify other type of delimiters (or make an instance of the Regexp class) you won't have this issue:
/^hello\/world$/ # escaping '/' just to say: "this is not the end"
%r"^hello/world$" # no need for escaping '/'
Regexp.new('^hello/world$') # no need for escaping '/'
Regexp.escape
Regexp.new(Regexp.escape('/domain/path'))
=> /\/domain\/path/
OR
Regexp.new(Regexp.escape('domain/path'))
=> /domain\/path/
精彩评论