JavaScript / ECMAScript

Redis Connection URL in JS

Match Redis connection URLs in both `redis://` and `rediss://` (TLS) forms, capturing user, password, host, port, and DB index.

Try it in the JS tester →

Pattern

regexJS
rediss?:\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/(\d+))?   (flags: g)

JavaScript / ECMAScript code

jsJavaScript
const re = new RegExp("rediss?:\\/\\/(?:([^:@\\s]+)(?::([^@\\s]*))?@)?([^:\\/\\s]+)(?::(\\d+))?(?:\\/(\\d+))?", "g");
const input = "REDIS_URL=redis://default:pass@redis.upstash.io:6379/0";
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

rediss?:\/\/ matches either scheme. The optional auth group captures user and optional password. The host group captures the hostname. (?::(\d+))? optionally captures the port. (?:\/(\d+))? optionally captures the database index (Redis databases are numeric).

Examples

Input

REDIS_URL=redis://default:pass@redis.upstash.io:6379/0

Matches

  • redis://default:pass@redis.upstash.io:6379/0

Input

rediss://cluster.example.com:6380

Matches

  • rediss://cluster.example.com:6380

Input

no redis url

No match

Same pattern, other engines

← Back to Redis Connection URL overview (all engines)