@cch137/format-utils
Version:
A collection of utility modules for formatting and processing data
10 lines (9 loc) • 423 B
JavaScript
export default function extractUrls(text, nonRepeated = true) {
const urlRegex = /((?:https?:\/\/)(?:www\.)?[a-zA-Z0-9\u4e00-\u9fa5-]+(?:\.[a-zA-Z0-9\u4e00-\u9fa5-]+)+(?:\/[^\s]*)?)/g;
const matches = text.match(urlRegex);
if (matches) {
const urls = matches.map((url) => /^https?:\/\//i.test(url) ? url : `http://${url}`);
return nonRepeated ? [...new Set(urls)] : urls;
}
return [];
}