DD
DevDash

Regex for URL (HTTP/HTTPS)

Regex Pattern

^https?:\/\/(?:[\w-]+\.)+[a-zA-Z]{2,}(?:\/[^\s]*)?$

Validates HTTP and HTTPS URLs

Quick Answer

The regex pattern for url (http/https) is `^https?:\/\/(?:[\w-]+\.)+[a-zA-Z]{2,}(?:\/[^\s]*)?$`. Validates HTTP and HTTPS URLs. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
https://example.com✓ Matches
http://subdomain.example.co.uk/path?q=1✓ Matches
https://example.io/api/v1/users/123✓ Matches
example.com✗ No match
ftp://example.com✗ No match
http://✗ No match

Code Examples

javascript

const regex = /^https?:\\/\\/(?:[\w-]+\.)+[a-zA-Z]{2,}(?:\\/[^\s]*)?$/;
const isValid = regex.test(value);

python

import re
pattern = r'^https?:\/\/(?:[\w-]+\.)+[a-zA-Z]{2,}(?:\/[^\s]*)?$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^https?:\/\/(?:[\w-]+\.)+[a-zA-Z]{2,}(?:\/[^\s]*)?$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^https?:\\/\\/(?:[\w-]+\.)+[a-zA-Z]{2,}(?:\\/[^\s]*)?$/', $value)) {
    echo "valid";
}

java

String pattern = "^https?:\\/\\/(?:[\\w-]+\\.)+[a-zA-Z]{2,}(?:\\/[^\\s]*)?$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.