I'm trying to figure out how to validate mongoose URIs with regex. My current regex is this:
^(mongodb:\/{2})(\w*?):{0,1}(\w*?)@{0,1}(\w+?):(\d+)\/(\w+?)
I need to be able to detect if a username and password is entered, i.e.
mongodb:username:password@localhost:27107/mydatabasename
So I need these cases to be invalid:
mongodb://username:localhost:27107/adfaeadf
mongodb://username:@localhost:27107/adfaeadf
mongodb://:password@localhost:27107/adfaeadf
but have these cases be valid:
mongodb://:@localhost:27107/mydatabasename
mongodb://@localhost:27107/mydatabasename
mongodb://localhost:27107/mydatabasename
mongodb:username:password@localhost:27107/mydatabasename
Any help would be appreciated
^(mongodb:(?:\/{2})?)((\w+?):(\w+?)@|:?@?)(\w+?):(\d+)\/(\w+?)$
http://regex101.com/r/mV2cR7/2 for the explanation
For your last link, is there supposed to be "//" after "mongodb:"? The regex will match it either way however.