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 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 locater
return !!pattern.test(str);
let url;
try {
url = new URL(str);
} catch (_) {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
};
export { combineUrls, isValidURL };