Web
Localhost URL Regex
During development, apps run on loopback addresses such as http://localhost:3000, http://127.0.0.1:8000 or the IPv6 form http://[::1]:8080. This pattern recognises exactly those hosts with an optional port and path, which is useful for detecting development callback URLs in OAuth settings, warning about localhost links left in production content and filtering local entries out of logs. Hosts that merely start with localhost, such as localhost.example.com, are rejected. The honest limit is that other loopback addresses such as 127.0.0.2 and custom local names are not covered.
Pattern (JavaScript)
/^https?://(?:localhost|127\.0\.0\.1|\[::1\])(?::[0-9]{1,5})?(?:/\S*)?$/Starts at the start of the line/string, then the text "http", then the character "s", optionally (zero or one time), then the text "://", then (the text "localhost" OR the text "127.0.0.1" OR the text "[::1]"), then (the character ":", then a digit, between 1 and 5 times), optionally (zero or one time), then (the character "/", then any character that is not whitespace, zero or more times), optionally (zero or one time), then the end of the line/string.
Examples
Matches
- ✓ http://localhost:3000
- ✓ https://127.0.0.1/api
- ✓ http://[::1]:8080/
Does not match
- ✗ http://example.com
- ✗ localhost:3000
- ✗ http://localhost.example.com
Try it
0 matches (JavaScript engine)
http://localhost:3000
https://127.0.0.1/api
http://[::1]:8080/
http://example.com
localhost:3000
http://localhost.example.comCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::[0-9]{1,5})?(?:\/\S*)?$/g;
const text = "http://localhost:3000";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^https?://(?:localhost|127\.0\.0\.1|\[::1\])(?::[0-9]{1,5})?(?:/\S*)?$')
text = 'http://localhost:3000'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '#^https?://(?:localhost|127\.0\.0\.1|\[::1\])(?::[0-9]{1,5})?(?:/\S*)?$#';
$text = 'http://localhost:3000';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^https?://(?:localhost|127\\.0\\.0\\.1|\\[::1\\])(?::[0-9]{1,5})?(?:/\\S*)?$");
String text = "http://localhost:3000";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^https?://(?:localhost|127\.0\.0\.1|\[::1\])(?::[0-9]{1,5})?(?:/\S*)?$`)
text := "http://localhost:3000"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^https?://(?:localhost|127\.0\.0\.1|\[::1\])(?::[0-9]{1,5})?(?:/\S*)?$");
var text = "http://localhost:3000";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^https?://(?:localhost|127\.0\.0\.1|\[::1\])(?::[0-9]{1,5})?(?:/\S*)?$").unwrap();
let text = "http://localhost:3000";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])(?::[0-9]{1,5})?(?:\/\S*)?$/
text = 'http://localhost:3000'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why does localhost.example.com not match?
- After the host, the pattern only allows a port, a path starting with / or the end of the string. A dot after localhost fails, so real domains that start with that word are not mistaken for local URLs.
- How can I include the whole 127.0.0.0/8 range?
- Replace 127\.0\.0\.1 with 127(?:\.[0-9]{1,3}){3}. That matches any address beginning with 127, which the operating system treats as loopback.
- Should production code block localhost URLs?
- Often yes, for example in webhook settings, to reduce server-side request forgery risk. A regex is one layer; resolving the hostname and checking the final IP is stronger.