Skip to main content
Network & Web · In-Browser Regular Expression Lab

Localhost Regex — Match Localhost and 127.0.0.1 Ports

Matches local development origins including http://localhost:PORT and 127.0.0.1 with ports 1024-65535.

Flags:
//none
Match Found
Load Sample:
Match Inspection & Details1 match
Match #1:http://localhost:3000

Test Samples Verification Matrix

Expected Valid Samples (Should Match)

http://localhost:3000 Pass
http://127.0.0.1:8080 Pass
https://localhost:443 Pass

Expected Invalid Samples (Must Reject)

http://castov.com Rejected (Pass)
ftp://localhost Rejected (Pass)
http://localhost:999999 Rejected (Pass)
Code Implementation Generator
// JavaScript / TypeScript (ES6+)
const pattern = /^https?:\/\/(?:localhost|127\.0\.0\.1)(?::[0-9]{1,5})?$/;
const text = "http://localhost:3000";

const isMatch = pattern.test(text);
console.log("Is Match:", isMatch);

if (isMatch) {
  const matches = text.match(pattern);
  console.log("Matches:", matches);
}

Regular Expression Token Breakdown

Matches local dev server origins for CORS and redirect URI whitelists. Each component of the regular expression serves a distinct syntactical purpose as mapped below:

Token PatternName / ComponentFunction & Rule
^https?:\/\/Protocolhttp:// or https://.
(?:localhost|127\.0\.0\.1)HostMatches localhost or 127.0.0.1.
(?::[0-9]{1,5})?$PortOptional port number up to 65535.

Frequently Asked Questions

Why use this in CORS middleware?
Allows local development servers to call APIs without compromising production origins.
Browse All 40 Patterns