Regex for CORS Origin
Regex Pattern
^(?:\*|https?:\/\/[a-zA-Z0-9.-]+(?::\d+)?)$CORS Access-Control-Allow-Origin value (* or origin URL)
Quick Answer
The regex pattern for cors origin is `^(?:\*|https?:\/\/[a-zA-Z0-9.-]+(?::\d+)?)$`. CORS Access-Control-Allow-Origin value (* or origin URL). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| * | ✓ Matches |
| https://example.com | ✓ Matches |
| http://localhost:3000 | ✓ Matches |
| example.com | ✗ No match |
| ftp://example.com | ✗ No match |
| *.example.com | ✗ No match |
Code Examples
javascript
const regex = /^(?:\*|https?:\\/\\/[a-zA-Z0-9.-]+(?::\d+)?)$/; const isValid = regex.test(value);
python
import re
pattern = r'^(?:\*|https?:\/\/[a-zA-Z0-9.-]+(?::\d+)?)$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^(?:\*|https?:\/\/[a-zA-Z0-9.-]+(?::\d+)?)$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^(?:\*|https?:\\/\\/[a-zA-Z0-9.-]+(?::\d+)?)$/', $value)) {
echo "valid";
}java
String pattern = "^(?:\\*|https?:\\/\\/[a-zA-Z0-9.-]+(?::\\d+)?)$"; 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
HTTP vs HTTPS URL
Matches HTTP or HTTPS URLs and captures the protocol
Data URL
Matches data: URL scheme with optional MIME type and base64 encoding