Created
April 26, 2021 13:04
-
-
Save samuelkarani/ca59415047906b664df7a171ea7c9ab7 to your computer and use it in GitHub Desktop.
This file contains 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
export function validURL(string) { | |
const pattern = new RegExp( | |
"^(https?:\\/\\/)?" + // protocol | |
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name | |
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address | |
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path | |
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string | |
"(\\#[-a-z\\d_]*)?$", | |
"i" | |
); // fragment locator | |
return !!pattern.test(string); | |
} | |
export function getURLS(string) { | |
var pattern = /[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)?/gi; | |
const repexp = new RegExp(pattern); | |
return string.match(repexp) || []; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment