FTP URL in JS
Match FTP and FTPS URLs, capturing optional credentials, host, port, and path.
Try it in the JS tester →Pattern
regexJS
ftps?:\/\/(?:([^:@\s]+)(?::([^@\s]*))?@)?([^:\/\s]+)(?::(\d+))?(?:\/\S*)? (flags: g)JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("ftps?:\\/\\/(?:([^:@\\s]+)(?::([^@\\s]*))?@)?([^:\\/\\s]+)(?::(\\d+))?(?:\\/\\S*)?", "g");
const input = "Source: ftp://anon:guest@files.example.com/pub/data.csv";
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
ftps?:\/\/ matches both `ftp://` and `ftps://`. The optional auth group captures user and password. The host group captures the server. (?::(\d+))? optionally captures the port (default is 21). (?:\/\S*)? optionally captures the path.
Examples
Input
Source: ftp://anon:guest@files.example.com/pub/data.csvMatches
ftp://anon:guest@files.example.com/pub/data.csv
Input
ftps://secure.host:990/privateMatches
ftps://secure.host:990/private
Input
no ftp linkNo match
—