开发者

Regexp.escape not escaping forward slashes?

开发者 https://www.devze.com 2022-12-19 10:36 出处:网络
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 so

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/
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号