在IRB中如果我将一个像“/ domain / path”这样的字符串传递给Regexp.escape,它就会返回相同的内容 . 我认为正斜杠应该用反斜杠转义?我在这里错过了什么吗?
此外,您需要转义 / 字符的唯一原因是因为它是regexp的分隔符,如果指定其他类型的分隔符(或创建Regexp类的实例),则不会出现此问题:
/
/^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/
2 回答
此外,您需要转义
/
字符的唯一原因是因为它是regexp的分隔符,如果指定其他类型的分隔符(或创建Regexp类的实例),则不会出现此问题:Regexp.escape
OR