isValidURL function update

master
Tudor Stanciu 2023-03-17 01:57:24 +02:00
parent 3443fbe685
commit 8449a3a14e
1 changed files with 7 additions and 10 deletions

View File

@ -7,16 +7,13 @@ const combineUrls = (piece1: string, piece2: string): string => {
}; };
const isValidURL = (str: string): boolean => { const isValidURL = (str: string): boolean => {
const pattern = new RegExp( let url;
"^(https?:\\/\\/)?" + // protocol try {
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name url = new URL(str);
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address } catch (_) {
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path return false;
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string }
"(\\#[-a-z\\d_]*)?$", return url.protocol === "http:" || url.protocol === "https:";
"i"
); // fragment locater
return !!pattern.test(str);
}; };
export { combineUrls, isValidURL }; export { combineUrls, isValidURL };