isValidURL function update
parent
3443fbe685
commit
8449a3a14e
17
src/utils.ts
17
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 };
|
||||
|
|
Loading…
Reference in New Issue