const regex = /^(mysqlx?:\/\/)(?:([\w$_]*)(?::([\w$_]+))?@)?([\w!#$%&'()*+,\-./;=?@[\]_~]*)(?::(\d{1,5}))?(?:\/[\w$_])?$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(mysqlx?:\\\/\\\/)(?:([\\w$_]*)(?::([\\w$_]+))?@)?([\\w!#$%&\'()*+,\\-.\/;=?@[\\]_~]*)(?::(\\d{1,5}))?(?:\\\/[\\w$_])?$', 'gm')
const str = `mysqlx://user@localhost:33065
mysql://user@localhost:3333
mysqlx://user@server.example.com/
mysqlx://user@198.51.100.14:123
mysqlx://user@[2001:db8:85a3:8d3:1319:8a2e:370:7348]
mysqlx://user@198.51.100.1/world%5Fx
mysqlx://user@198.51.100.2:33060/world
ssluser@127.0.0.1?ssl-ca%3D%2Froot%2Fclientcert%2Fca-cert.pem%26ssl-cert%3D%2Fro\\
ot%2Fclientcert%2Fclient-cert.pem%26ssl-key%3D%2Froot%2Fclientcert%2Fclient-key
.pem
mysqlx://user:@localhost
`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions