URL with Port Regex
Validates an HTTP or HTTPS URL that explicitly includes a port number after the hostname, with an optional path, query string, or fragment.
Regex Pattern
^https?:\/\/[a-zA-Z0-9.-]+:\d{2,5}(\/[^\s]*)?$Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| ^ | Anchors the match to the start of the string |
| https? | Matches http or https (the trailing s is optional) |
| :\/\/ | Literal :// scheme separator |
| [a-zA-Z0-9.-]+ | Hostname made of letters, digits, dots, and hyphens |
| :\d{2,5} | A literal colon followed by a 2-to-5-digit port number, required rather than optional |
| (\/[^\s]*)? | Optional path, query string, or fragment starting with a slash |
| $ | Anchors the match to the end of the string |
Detailed Explanation
What it does
This pattern validates that a string is an absolute HTTP or HTTPS URL that explicitly names a port number, such as https://example.com:8080/path. Unlike a general URL pattern, the port segment here is mandatory, not optional.
Why it works
The scheme and hostname are matched the same way a general URL pattern would, but the colon-and-digits port group is placed directly in the main sequence rather than wrapped in an optional group, so the overall match fails whenever no port is present. Anchoring with ^ and $ ensures the whole string, not just a substring, must fit this shape.
Common use cases
- Validating configuration values for services where the port must be explicit, like internal API endpoints
- Parsing log lines or connection strings that record host:port pairs as URLs
- Filtering a list of URLs to find only those pointing at non-default ports
- Form validation for developer-facing tools where users must supply host and port together
Edge cases
- Default ports like :80 or :443 are matched even though they are redundant for their scheme
- Very long port numbers like :99999 exceed the valid 0-65535 range but still match digit-count-wise since the regex only checks digit count, not numeric range
- IPv4 hosts such as https://192.168.1.1:8080 match because digits and dots are allowed in the hostname class
- A URL without a port such as https://example.com is correctly rejected
Limitations
- Does not validate that the port number is within the valid 0-65535 range
- Does not support IPv6 literal hosts in bracket notation like [::1]:8080
- Only recognizes http and https schemes
- Does not validate percent-encoding in the path or query string
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const urlWithPortRegex = /^https?:\/\/[a-zA-Z0-9.-]+:\d{2,5}(\/[^\s]*)?$/;
console.log(urlWithPortRegex.test('https://example.com:8080/path')); // trueCommon Mistakes
Making the port group optional with (:\d+)? and expecting it to reject bare hostnames
Fix: Remove the outer optional group around the colon and digits so the port becomes mandatory in the sequence
Assuming any 2-to-5-digit sequence is a valid port number
Fix: Add an application-level numeric range check (0-65535) after the regex match succeeds
Forgetting IPv6 hosts use bracket notation like [::1]:8080, which this pattern does not support
Fix: Write a separate branch or pattern for bracketed IPv6 hosts if they need to be accepted
Performance Notes
- Anchoring with ^ and $ prevents the engine from scanning for a match at every position in a long string
- The hostname character class [a-zA-Z0-9.-]+ is a simple bounded quantifier with no nested repetition, so it does not risk catastrophic backtracking
- The trailing [^\s]* is greedy but unambiguous with its neighbors, keeping matching linear in the length of the input
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |