URL Validation
Match http and https URLs with optional www prefix, paths, query strings, and fragments.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("https?:\\/\\/(?:www\\.)?[\\w\\-]+(?:\\.[\\w\\-]+)+[\\w\\-._~:/?#\\[\\]@!$&'()*+,;=%]*", "gi");
const input = "https://www.example.com";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"https?:\/\/(?:www\.)?[\w\-]+(?:\.[\w\-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=%]*", re.IGNORECASE)
input_text = "https://www.example.com"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`(?i)https?:\/\/(?:www\.)?[\w\-]+(?:\.[\w\-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=%]*`)
input := `https://www.example.com`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
https?:\/\/(?:www\.)?[\w\-]+(?:\.[\w\-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=%]* (flags: gi)Raw source: https?:\/\/(?:www\.)?[\w\-]+(?:\.[\w\-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=%]*
How it works
Examples
Input
https://www.example.comMatches
https://www.example.com
Input
http://api.example.org/v1/users?id=42Matches
http://api.example.org/v1/users?id=42
Input
not a urlNo match
—Common use cases
- •Extracting links from text or HTML
- •Validating user-submitted URLs
- •Web scraping and crawling
- •Security scanning for external links
Related patterns
Twitter / X URL
WebMatch Twitter/X profile and status URLs, capturing the handle and (optional) tweet ID.
GitHub Repository URL
WebMatch GitHub repository URLs and capture the owner and repo segments.
LinkedIn Profile URL
WebMatch LinkedIn profile URLs and capture the profile slug.
URL Query String
WebExtract the query string portion of a URL (everything between `?` and `#` or end-of-string).
Related concepts
Regex Flags in JavaScript: g, i, m, s, u, y
ConceptFlags modify regex behavior globally. g enables global matching, i makes it case-insensitive, m changes anchor behavior, s dots match newlines, u enables Unicode, y is sticky.
How to Extract URLs from Text with Regex
How-toUse a permissive URL pattern with the g flag and String.matchAll. A practical regex accepts http/https, optional www, and common URL characters.