Web
HTTP or HTTPS URL Regex
This pattern checks that a whole input is an absolute web address starting with http:// or https://. After the scheme it requires a host made of letters, digits, dots and dashes, then allows an optional port and any path, query string or fragment that contains no whitespace. It is a sensible check for website fields in profile forms, link submission tools and configuration screens. The honest limit is that it is intentionally permissive about the host, so values like http://localhost or a host with a trailing dot still match, and internationalised domain names written in Unicode are rejected.
Pattern (JavaScript)
/^https?://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[^\s]*)?$/Starts at the start of the line/string, then the text "http", then the character "s", optionally (zero or one time), then the text "://", then one of the characters A-Za-z0-9.-, one or more times, then (the character ":", then a digit, between 1 and 5 times), optionally (zero or one time), then (the character "/", then any character that is not a whitespace character, zero or more times), optionally (zero or one time), then the end of the line/string.
Limit: Checks the format only; it cannot tell whether the page exists or is safe.
Examples
Matches
- ✓ https://example.com
- ✓ http://www.example.org/path?q=1#top
- ✓ https://example.com:8080/a
Does not match
- ✗ ftp://example.com
- ✗ example.com
- ✗ https://exa mple.com
Try it
0 matches (JavaScript engine)
https://example.com
http://www.example.org/path?q=1#top
https://example.com:8080/a
ftp://example.com
example.com
https://exa mple.comCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^https?:\/\/[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:\/[^\s]*)?$/g;
const text = "https://example.com";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^https?://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[^\s]*)?$')
text = 'https://example.com'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#^https?://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[^\s]*)?$#';
$text = 'https://example.com';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^https?://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[^\\s]*)?$");
String text = "https://example.com";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^https?://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[^\s]*)?$`)
text := "https://example.com"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^https?://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[^\s]*)?$");
var text = "https://example.com";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^https?://[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:/[^\s]*)?$").unwrap();
let text = "https://example.com";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^https?:\/\/[A-Za-z0-9.-]+(?::[0-9]{1,5})?(?:\/[^\s]*)?$/
text = 'https://example.com'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why are URLs without http or https rejected?
- Requiring a scheme removes ambiguity about how the link should be opened. If you want to accept bare domains, prepend https:// when the input has no scheme and then test it.
- Should I use the URL constructor instead?
- In JavaScript, new URL(value) is an excellent second step because it parses the address fully. The regex is still useful for quick form feedback and for languages without a built-in parser.
- How do I find URLs inside a paragraph?
- Remove the ^ and $ anchors and use the g flag. Trailing punctuation such as a full stop may be captured, so trim characters like . , ) from the end of each match.