1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-25 23:16:41 +07:00
vk-tg-bot/src/utils/extract.ts
2025-02-24 19:48:21 +07:00

17 lines
527 B
TypeScript

import { URL } from "url";
const urlRe = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s\]]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s\]]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s\]]{2,}|www\.[a-zA-Z0-9]+\.[^\s\]]{2,})/gim;
export const extractURLs = (text: string): URL[] => {
const matches = text.match(urlRe) || [];
return matches
.map((m) => {
try {
return new URL(m);
} catch (e) {
return;
}
})
.filter((el) => el) as URL[];
};