URL regex
Matches http and https links in running text. Group 1 is the scheme, group 2 the host, group 3 an optional port and group 4 the path, which may not end in a full stop, comma or closing bracket, so a link at the end of a sentence comes out clean.
The pattern
//gi
Sample text, 3 matches
Docs live at https://regex-craft.example.io/docs, the status page at http://status.internal:8080/healthz, and the raw spec at https://cdn.example.net/assets/schema-v2.json.
Point at a coloured group to see it in the pattern, the text and the explanation.
What it deliberately doesn’t do
- The host needs at least one dot, so http://localhost is not matched.
- Other schemes (ftp, mailto, file) are ignored on purpose.
- It finds links; it does not validate them. Use the URL constructor for that.
Explained, in order
(https?)Group 1.Capturing group #1 — groups the pattern and captures the matched text for later referencehttpMatches the text "http" literallysMatches the character 's' literally?The preceding element matches optionally (zero or one time) (greedy)://Matches the text "://" literally([\w-]+(?:\.[\w-]+)+)Group 2.Capturing group #2 — groups the pattern and captures the matched text for later reference[\w-]Character class — matches one character from: a word character (letter, digit, or underscore), '-'+The preceding element matches one or more times (greedy)(?:\.[\w-]+)Non-capturing group — groups the pattern without capturing\.Matches the character '.' literally[\w-]Character class — matches one character from: a word character (letter, digit, or underscore), '-'+The preceding element matches one or more times (greedy)+The preceding element matches one or more times (greedy)(?::(\d+))Non-capturing group — groups the pattern without capturing:Matches the character ':' literally(\d+)Group 3.Capturing group #3 — groups the pattern and captures the matched text for later reference\dMatches a digit (0–9)+The preceding element matches one or more times (greedy)?The preceding element matches optionally (zero or one time) (greedy)(/[^\s]*[^\s.,;:!?)\]])Group 4.Capturing group #4 — groups the pattern and captures the matched text for later reference/Matches the character '/' literally[^\s]Character class — matches any single character NOT in: a whitespace character (space, tab, newline, etc.)*The preceding element matches zero or more times (greedy)[^\s.,;:!?)\]]Character class — matches any single character NOT in: a whitespace character (space, tab, newline, etc.), '.', ',', ';', ':', '!', '?', ')', ']'?The preceding element matches optionally (zero or one time) (greedy)
In your language
JavaScript
The engine this page was tested with.
const re = /(https?):\/\/([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(\/[^\s]*[^\s.,;:!?)\]])?/gi;
// Example
const matches = [...str.matchAll(/(https?):\/\/([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(\/[^\s]*[^\s.,;:!?)\]])?/gi)];
for (const m of matches) {
console.log(m[0], m.index, m.groups);
}Python
Translated. Named groups are rewritten to (?P<name>…).
import re
pattern = re.compile(r"(https?)://([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(/[^\s]*[^\s.,;:!?)\]])?", re.IGNORECASE)
# Example
for m in pattern.finditer(text):
print(m.group(0), m.start(), m.groups())Go
Translated. Go's RE2 has no lookaround or backreferences.
package main
import (
"fmt"
"regexp"
)
// Note: Inline flags at the start of the pattern: (?i), (?m), (?s).
var re = regexp.MustCompile(`(?i)(https?)://([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(/[^\s]*[^\s.,;:!?)\]])?`)
func main() {
for _, match := range re.FindAllStringSubmatch(text, -1) {
fmt.Println(match[0], match)
}
}Rust
Translated. The regex crate has no lookaround or backreferences.
use regex::Regex;
// Note: Rust finds all matches via find_iter / captures_iter (no 'g' flag).
fn main() {
let re = Regex::new(r#"(?i)(https?)://([\w-]+(?:\.[\w-]+)+)(?::(\d+))?(/[^\s]*[^\s.,;:!?)\]])?"#).unwrap();
for m in re.find_iter(text) {
println!("{} at {}", m.as_str(), m.start());
}
}