Generic Connection String (URL Form) in JS
Parse generic URL-form connection strings: `protocol://[user[:pass]@]host[:port][/database]`.
Try it in the JS tester →Pattern
regexJS
([a-zA-Z][\w+\-.]*):\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/([^?\s]*))? (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("([a-zA-Z][\\w+\\-.]*):\\/\\/(?:([^:@\\s]+)(?::([^@\\s]*))?@)?([^:\\/\\s]+)(?::(\\d+))?(?:\\/([^?\\s]*))?", "g");
const input = "postgres://admin:s3cret@db.example.com:5432/main";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
Group 1 captures the scheme (postgres, mysql, redis, mongodb, kafka, etc.). The optional auth section captures user and optional password. Group 4 is the host. Optional port and path captures follow. This is a working starting point for parsing many SaaS connection strings; for production use a real URL parser.
Examples
Input
postgres://admin:s3cret@db.example.com:5432/mainMatches
postgres://admin:s3cret@db.example.com:5432/main
Input
kafka://broker.internal:9092Matches
kafka://broker.internal:9092
Input
no connection hereNo match
—