【发布时间】:2017-12-04 11:03:55
【问题描述】:
我正在尝试创建一个正则表达式以仅匹配 rails 中的索引 url(带或不带参数)。
以下三个符合我的预期:
regex = /^http:\/\/localhost:3000\/v2\/manufacturers\/?(\S+)?$/
regex.match?('http://localhost:3000/v2/manufacturers?enabled=true')
#=> true
regex.match?('http://localhost:3000/v2/manufacturers/')
#=> true
regex.match?('http://localhost:3000/v2/manufacturers')
#=> true
我希望正则表达式不匹配这些:
regex.match?('http://localhost:3000/v2/manufacturers/1')
#=> true
regex.match?('http://localhost:3000/v2/manufacturers/123')
#=> true
regex.match?('http://localhost:3000/v2/manufacturers/1?enabled=true')
#=> true
编辑:
很抱歉,我忘了说它应该匹配:
regex.match?('http://localhost:3000/v2/manufacturers/1/models')
因为它是一个有效的索引 url
【问题讨论】:
-
你有什么问题?
-
@WiktorStribiżew 完成,谢谢
标签: ruby-on-rails ruby regex