JAVASCRIPT
Extract All URLs from Text
Discover how to use a JavaScript regex pattern to efficiently find and extract all complete URLs (http/https) embedded within a larger string of text.
function extractUrls(text) {
const urlRegex = new RegExp(
/(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?:\/\/[a-zA-Z0-9]+\.[^\s]{2,}|[a-zA-Z0-9]+\.[^\s]{2,})/gi
);
return text.match(urlRegex) || [];
}
// Examples:
// const text = "Visit our website at https://example.com or check out www.google.com for search. Also, another one: http://dev.to/some-article";
// console.log(extractUrls(text));
// Expected: ["https://example.com", "www.google.com", "http://dev.to/some-article"]
How it works: This `extractUrls` function in JavaScript utilizes a regular expression to search for and extract all occurrences of valid URLs (starting with `http://`, `https://`, or `www.`) from a given text string. The `match()` method with the global flag `g` ensures all matches are returned as an array; if no URLs are found, an empty array is returned.