JAVASCRIPT
Validating and Extracting URLs with Regex
Discover how to validate URLs and extract them from text using JavaScript and a regular expression, essential for link handling and content processing.
const extractUrls = (text) => {
// Regex to find URLs (http/https, optional www, domain, TLD, optional path/query)
const urlRegex = /(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) || [];
};
const textWithUrls = "Visit our site at https://www.example.com or check out http://blog.test.org for more info.";
console.log(extractUrls(textWithUrls)); // ["https://www.example.com", "http://blog.test.org"]
How it works: This JavaScript function `extractUrls` uses a regular expression to find and extract all URLs present within a given text string. The regex is designed to match common URL patterns, including `http` or `https` protocols, optional `www` subdomains, domain names, top-level domains, and optional paths or query parameters. It returns an array of found URLs.