开发者

how to regex a list of url paths?

开发者 https://www.devze.com 2023-03-30 01:08 出处:网络
I have a list of url paths: WHITELIST_PA开发者_运维技巧THS = [ \'/assets\', \'/images\', \'/javascripts\']

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

精彩评论

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