diff --git a/src/utils.ts b/src/utils.ts index 1828553..c30fdbc 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 };