开发者

how to regex a list of url paths?

I have a list of url paths:

WHITELIST_PA开发者_运维技巧THS = [ '/assets', '/images', '/javascripts']

How can regex be used to do something like:

allow_access = WHITELIST_PATHS.include? '/assets/application.css'

Idea being that the tested path just needs to start with a whitelist path. Ideas? Thanks


allow_access = WHITELIST_PATHS.any? {|p| '/assets/application.css'.start_with? p }


WHITELIST_PATHS = [ '/assets', '/images', '/javascripts']
# probably should be 
# WHITELIST_PATHS = [ '/assets/', '/images/', '/javascripts/']
WHITELIST_REGEXP = /^(#{WHITELIST_PATHS.join("|")})/

allow_access = !!('/assets/application.css' =~ WHITELIST_REGEXP)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜