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
| Input | Result |
|---|---|
| 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
Subdomain Extraction
Extracts the subdomain from a URL or domain name
Protocol Detection
Detects and captures the protocol from a URL
Port Number
Extracts port number from a URL or host string
Query Parameter
Extracts key-value pairs from URL query strings
Data URL
Matches data: URL scheme with optional MIME type and base64 encoding
WebSocket URL
WebSocket URL starting with ws:// or wss://