Dany
Dany

Reputation: 2800

Regular expression to validate '/' in URL

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

Answers (2)

Denys Séguret
Denys Séguret

Reputation: 382284

You seem to want optional groups:

"^/(home|about)?(/[a-zA-Z0-9]+)?$"

Demonstration

Upvotes: 1

VonC
VonC

Reputation: 1327164

Why not adding that case as an alternative:

"^/$|(/(home|about)/(|[a-zA-Z0-9]+)$)"

Upvotes: 3

Related Questions