JAVASCRIPT

Extract All URLs from a Text String

Efficiently extract all full URLs (http, https, www) from any given text string using a powerful JavaScript regex pattern for link parsing.

function extractUrls(text) {
  const urlRegex = /\b((https?:\/\/|www\.)[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi;
  return text.match(urlRegex) || [];
}

// Example:
const textWithUrls = "Check out our website: https://example.com and www.google.com. Also, this is a link: http://sub.domain.org/path?query=1#hash";
const urlsFound = extractUrls(textWithUrls);
console.log(urlsFound); // ["https://example.com", "www.google.com", "http://sub.domain.org/path?query=1#hash"]
How it works: This JavaScript function extracts all complete URLs from a given string. The regex `\b((https?:\/\/|www\.)[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])` looks for word boundaries (`\b`) and then matches either `http://`, `https://`, or `www.` followed by common URL characters. The `g` flag ensures all matches are found, and `i` makes it case-insensitive. The `match()` method returns an array of all found URLs or `null` if none are found, which is handled by returning an empty array.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs