Skip to content

Instantly share code, notes, and snippets.

@matiaslopezd
Last active December 1, 2020 19:09
Show Gist options
  • Save matiaslopezd/dbd5e75f159e4f9d7b240e924ca670a8 to your computer and use it in GitHub Desktop.
Save matiaslopezd/dbd5e75f159e4f9d7b240e924ca670a8 to your computer and use it in GitHub Desktop.
Get FQDN with Regex
/**
* Get FQDN from a string
* @name FQDN
* @param URL {String} - String you want get FQDN
* @return {String}
**/
function FQDN(URL = '') {
URL = URL.subtring(0, 50); // This will avoid evaluate long malicious fqdn
const regex = /[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+/i;
const filtered = regex.exec(URL);
if (!filtered) throw new Error(`Input ${URL} is not a valid URL.`);
const [domain] = (Array.isArray(filtered)) ? filtered : [];
return domain;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment