Last active
December 1, 2020 19:09
-
-
Save matiaslopezd/dbd5e75f159e4f9d7b240e924ca670a8 to your computer and use it in GitHub Desktop.
Get FQDN with Regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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