开发者

How do you validate against more than one regex?

开发者 https://www.devze.com 2023-03-01 10:02 出处:网络
I have this validation: validates :url,:uniqueness => true, :format => { :with => /^(http:\\/\\/)?(www\\.)?youtube\\.com\\/watch\\?v=([开发者_如何学编程a-zA-Z0-9_-]*)/}

I have this validation:

validates :url,     :uniqueness => true,
                    :format => { :with => /^(http:\/\/)?(www\.)?youtube\.com\/watch\?v=([开发者_如何学编程a-zA-Z0-9_-]*)/}

I want to let url match either the regex above or another regex. How do I add this second regex?


You put both regexes into one regex using the 'bar' operator which does an 'or' for you:

/(^(http:\/\/)?(www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9_-]*)|helloworld)/

Matches a URL or a string containing "helloworld"

0

精彩评论

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