IP Address Regex Javascript Validator

Search...

⌘K

IP Address Regex Javascript Validator

Search...

⌘K


IP Address Regex Javascript Validator

IP Address Regex Javascript Validator

Validate IPv4 and IPv6 patterns effortlessly with the IP Address Regex JavaScript Validator on Qodex. Whether you’re working with user-submitted data or backend APIs, this tool ensures your IP addresses meet standard formats. You can also test complex patterns using the JavaScript Regex Tester, or verify other inputs like MAC addresses with the MAC Address Regex JavaScript Validator and email formats with the Email Regex JavaScript Validator. Perfect for developers building secure and accurate data validation logic.

127.0.0.1
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "127.0.0.1" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

IP Address Regex JavaScript Validator


What is IP Address Regex?


An IP address is a unique string that identifies a device on a network. It comes in two forms: IPv4 and IPv6. Validating IP addresses using regular expressions (regex) in JavaScript helps ensure accurate and secure input for forms, APIs, and configuration settings.


Regex is especially useful in frontend validation where preventing malformed IP entries can reduce backend errors and security issues.


Regex Patterns for IP Address Validation


1. IPv4 Address


IPv4 consists of four groups of 1 to 3 digits (0–255), separated by dots.


Regex Pattern (IPv4):

^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d


Matches: 192.168.1.1, 127.0.0.1, 255.255.255.255

Invalid: 300.168.1.1, 192.168.1, abcd.123.0.1


2. IPv6 Address


IPv6 is a longer hexadecimal-based format, typically written in 8 groups separated by colons.


Regex Pattern (IPv6 - simplified):

^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$


Matches: 2001:0db8:85a3:0000:0000:8a2e:0370:7334

Invalid: 2001:db8:85a3::8a2e:370g:7334


⚠️ Note: There are more advanced patterns to match IPv6 shorthand and mixed forms; the above works for the full, expanded format.


How to Validate IP Address in JavaScript Using Regex


function isValidIPv4(ip) {
  const ipv4Regex = /^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$/;
  return ipv4Regex.test(ip);
}

function isValidIPv6(ip) {
  const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
  return ipv6Regex.test(ip);
}

// Example usage
console.log(isValidIPv4("192.168.0.1"));    // true
console.log(isValidIPv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334")); // true


Use Cases


  • Web Forms: Validate IP address inputs before submission.

  • Network Monitoring Apps: Match valid device IPs using JavaScript Regex Tester.

  • APIs and Admin Panels: Ensure safe backend routing or configuration using validated IPs.

  • Log Analysis: Extract IP addresses from server logs using the String Extractor.


Pro Tips

  • Always use anchors (^ and $) to ensure the entire string matches the pattern.

  • IPv6 has many valid shorthand notations; if you need to support them, consider libraries or advanced regex.

  • Use trim() on inputs before testing to avoid whitespace mismatches.

  • For bulk validation or testing multiple formats, combine this tool with the JavaScript Regex Tester.

Frequently asked questions

Can this regex detect invalid private IP ranges?×
No, it only validates the format, not the IP range. Logic-based filtering must be used separately.
What’s the difference between IPv4 and IPv6 regex patterns?+
Should I use regex or a library for IP validation?+
Can I modify the pattern to allow port numbers?+
Is this validator case-sensitive?+