Reputation: 2800
I am writing a web application in golang. I am using regular expression to validate the URL. But I am not able to include '/' in the URL validation.
var validPath = regexp.MustCompile("^/(home|about)/(|[a-zA-Z0-9]+)$")
The above URL takes '/home/', '/about/' but could not make for '/'.
Could anyone please help me on this?
Upvotes: 0
Views: 139
Reputation: 382284
You seem to want optional groups:
"^/(home|about)?(/[a-zA-Z0-9]+)?$"
Upvotes: 1
Reputation: 1327164
Why not adding that case as an alternative:
"^/$|(/(home|about)/(|[a-zA-Z0-9]+)$)"
Upvotes: 3