regex match URL — http, https, and optional path
Quick Answer
# Match http/https URLs
/https?:\/\/[^\s<>"{}|\\^\[\]]+/
// JavaScript — extract all URLs from text
const text = 'Visit https://example.com/path?q=1 or http://foo.io';
text.match(/https?:\/\/[^\s<>"{}|\\^\[\]]+/g)
// ['https://example.com/path?q=1', 'http://foo.io']
Usage
Extract hyperlinks from plain text, validate URL input, or detect URLs in user content.
Other causes & fixes
Stricter URL pattern with domain validation
/https?:\/\/(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(?:\/[^\s]*)?/
// Matches:
// https://example.com
// https://sub.domain.co.uk/path/to/page?query=1#fragment
// Does not match:
// http://localhost (no TLD)
Python — extract URLs
import re
URL_RE = re.compile(r'https?://[^\s<>"{}|\\^\[\]]+')
text = 'See https://example.com/path for details'
urls = URL_RE.findall(text)
# ['https://example.com/path']
Validate a complete URL with named groups
/^(?P<scheme>https?):\/\/(?P<host>[^\/?#]+)(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/
Prefer URL() constructor for validation in JS
function isValidUrl(str) {
try {
new URL(str);
return true;
} catch {
return false;
}
}
isValidUrl('https://example.com'); // true
isValidUrl('not-a-url'); // false
Related