🔍Regex Recipes

Port Number (1-65535)

Validate network port numbers in valid range 1-65535.

Pattern

^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$

Explanation

Validates port numbers from 1 to 65535. Excludes port 0 as it's typically reserved.

Examples

HTTP
Input
80
Output
✓ Match
HTTPS
Input
443
Output
✓ Match
Custom
Input
8080
Output
✓ Match
Maximum
Input
65535
Output
✓ Match
Invalid - zero
Input
0
Output
✗ No match
Invalid - too large
Input
65536
Output
✗ No match

Code Examples

JavaScript
const portRegex = /^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;

function isValidPort(port) {
  const num = parseInt(port, 10);
  return num >= 1 && num <= 65535;
}

const valid = isValidPort('8080'); // true

Try it Now

💡 Tips

  • Common ports: 80 (HTTP), 443 (HTTPS), 22 (SSH), 3306 (MySQL)
  • Development often uses 3000, 8000, 8080
  • Ephemeral ports typically 49152-65535
  • Consider simple numeric range check instead

⚠️ Common Pitfalls

  • Port 0 is reserved and typically invalid
  • Ports 1-1023 are privileged (need root/admin)
  • Some ports are blocked by browsers
  • Numeric validation may be simpler than regex