Tallboy
Tallboy

Reputation: 13407

Validate with two regexes?

I have a complicated URL that can come in two different formats:

http://site.com/app_id/{app_id}/user_id/{user_id}

and also in the format of:

site:app_id:{app_id}:user_id:{user_id}

This is what I have so far:

/http:\/\/site\.com\/app_id\/[\w]+\/user_id\/[\w]+/ig

I have no idea how to write the regular expression to handle the second URL. I tried using (stuff)? but it seems too messy. It would be great to just validate on one OR the other.

Upvotes: 0

Views: 62

Answers (2)

jTC
jTC

Reputation: 1350

I would use an explicit or statement to ensure that you aren't breaking one regex mode to fix another.

Might want to try something like this:

/[http:\/\/site\.com\/app_id\/[\w]+\/user_id\/[\w]|http:\/\/site:app_id:[\w]:user_id:[\w]]/ig

Upvotes: 1

ziesemer
ziesemer

Reputation: 28687

Use the "or" operator, a pipe:

|

Upvotes: 3

Related Questions