开发者

Regexp.escape not escaping forward slashes?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜