用于rack-ssl-enforcer的正则表达式ruby

时间:2014-01-31 19:36:02

标签: ruby regex

使用此正则表达式:

config.middleware.use Rack::SslEnforcer, :only => %r{^\/[a-z][a-z]+\/users/sign_in}, :strict => true

^\/[a-z][a-z]+\/users/sign_in

它适用于:

/en/users/sign_in
/es/users/sign_in
/de/users/sign_in
/fr/users/sign_in

我想为网址添加有效的正则表达式:

/en-us/users/sign_in
/en/users/sign_in
/en-ca/users/sign_in
/zh-cn/users/sign_in
.
.
.

1 个答案:

答案 0 :(得分:1)

添加可选的非捕获组:

^/[a-z]{2}(?:-[a-z]{2})?/users/sign_in

注意:这会将country / lang代码限制为两个字符。如果您想要“两个或更多”,请更改为{2,}