DD
DevDash

Regex for HTTP vs HTTPS URL

Regex Pattern

^(https?):\/\/[^\s]+$

Matches HTTP or HTTPS URLs and captures the protocol

Quick Answer

The regex pattern for http vs https url is `^(https?):\/\/[^\s]+$`. Matches HTTP or HTTPS URLs and captures the protocol. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
http://example.com✓ Matches
https://secure.example.com/path✓ Matches
http://api.example.com:8080✓ Matches
ftp://files.com✗ No match
example.com✗ No match
ws://socket.io✗ No match

Code Examples

javascript

const regex = /^(https?):\\/\\/[^\s]+$/;
const isValid = regex.test(value);

python

import re
pattern = r'^(https?):\/\/[^\s]+$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^(https?):\/\/[^\s]+$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^(https?):\\/\\/[^\s]+$/', $value)) {
    echo "valid";
}

java

String pattern = "^(https?):\\/\\/[^\\s]+$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.