URL Validation Regex (Strict with Protocol)
• CronOS Team
regexurlvalidationtutorialwebstrict
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
URL Validation Regex (Strict with Protocol)
A strict regex pattern for validating URLs that requires protocol (http/https) and validates domain, TLD, and path components.
Pattern Breakdown
regex
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
Components
| Component | Description | Matches |
|---|---|---|
https? | Protocol | http or https (required) |
:\/\/ | Protocol separator | Literal :// |
(www\.)? | Optional www | Optional www. subdomain |
[-a-zA-Z0-9@:%._\+~#=]{1,256} | Domain/host | 1-256 chars: letters, digits, special chars |
\. | TLD separator | Literal dot |
[a-zA-Z0-9()]{1,6} | TLD | 1-6 letters, digits, or parentheses |
\b | Word boundary | Ensures TLD completion |
([-a-zA-Z0-9()@:%_\+.~#?&//=]*) | Optional path | Zero or more path/query characters |
Character Classes
https?-httporhttps(s is optional, but protocol required)[-a-zA-Z0-9@:%._\+~#=]- Domain characters: letters, digits, hyphens, and various special chars[a-zA-Z0-9()]- TLD characters: letters, digits, parentheses[-a-zA-Z0-9()@:%_\+.~#?&//=]- Path/query characters: various URL-safe characters?- Quantifier: zero or one occurrence{1,256}- Quantifier: between 1 and 256 occurrences{1,6}- Quantifier: between 1 and 6 occurrences*- Quantifier: zero or more occurrences\b- Word boundary anchor
Examples
Valid:
https://example.comhttp://www.example.comhttps://subdomain.example.orghttps://example.com/path/to/pagehttps://example.com?key=valuehttps://example.com#sectionhttps://user@example.comhttps://example.co.uk
Invalid:
example.com(missing protocol)ftp://example.com(only http/https allowed)https://example(missing TLD)https://.com(missing domain)http://example..com(double dot)
Implementation
JavaScript
javascript
const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;
urlRegex.test('https://example.com'); // true
urlRegex.test('http://www.example.com/path'); // true
urlRegex.test('example.com'); // false (missing protocol)
urlRegex.test('ftp://example.com'); // false (wrong protocol)
Python
python
import re
url_regex = r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)'
bool(re.match(url_regex, 'https://example.com')) # True
bool(re.match(url_regex, 'http://www.example.com/path')) # True
bool(re.match(url_regex, 'example.com')) # False (missing protocol)
Go
go
urlRegex := regexp.MustCompile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)`)
urlRegex.MatchString("https://example.com") // true
urlRegex.MatchString("http://www.example.com/path") // true
urlRegex.MatchString("example.com") // false (missing protocol)
Limitations
- Protocol required: Must include
http://orhttps:// - No port numbers: Doesn't support
:8080port specification - No IPv4/IPv6: Doesn't validate IP addresses as hosts
- TLD validation: Basic TLD check, doesn't validate against known TLD list
- Path validation: Allows various characters but doesn't validate path structure
- No fragment validation: Fragment part (
#section) not explicitly validated
When to Use
- Strict URL validation requiring protocol
- When you need to ensure URLs have http/https
- API endpoints that require full URLs
- Web scraping input validation
- When protocol is mandatory
For production, consider:
- Using URL parsing libraries for comprehensive validation
- Adding port number support
- Validating against known TLD lists
- Supporting additional protocols if needed
- Combining with URL parsing for full validation
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!