Creating a regex for mongoose URI Creating a regex for mongoose URI mongoose mongoose

Creating a regex for mongoose URI


^(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.


If you want to support passing arguments:

/^mongodb:\/\/(?:(?:(\w+)?:(\w+)?@)|:?@?)((?:[\w.-])+)(?::(\d+))?(?:\/([\w-]+))?(?:\?([\w-]+=[\w-]+(?:&[\w-]+=[\w-]+)*)?)?$/


this won't match for servers of the format server-a1.host.com (e.g. other than localhosts) or replica sets, try:

^(mongodb:(?:\/{2})?)((\w+?):(\w+?)@|:?@?)(\S+?):(\d+)\/(\S+?)(\?replicaSet=(\S+?))?$

explanation here: https://regex101.com/r/mV2cR7/47