What's new
  • Do not create multi-accounts, you will be blocked! For more information about rules, limits, and more, visit the Help Page Found a dead link? Use the report button!
  • If no files are available, please contact me or reply in the thread. I will update them immediately.

Unicast ipv4 without specify port regex rule [closed]

To match a unicast IPv4 address (without specifying a port) using a regular expression (regex), here’s a solution that considers valid unicast IPv4 addresses.


---

Regex Rule

JavaScript:
\b((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\b





---

Explanation

1. Match each octet of an IPv4 address:

25[0-5]: Matches numbers from 250 to 255.

2[0-4][0-9]: Matches numbers from 200 to 249.

1[0-9]{2}: Matches numbers from 100 to 199.

[1-9]?[0-9]: Matches numbers from 0 to 99.

Combine with dots (\.) for all four octets.



2. Match unicast IPv4:

Unicast IPv4 addresses range from 1.0.0.0 to 223.255.255.255, excluding multicast and special addresses:

0.x.x.x is invalid (reserved).

127.x.x.x is reserved for loopback.

224.x.x.x and above are multicast or reserved.



However, the regex above does not enforce strict unicast validation—it simply ensures valid IPv4 format. To enforce unicast rules programmatically, filter the matches in your code.


3. Avoid ports:

Ensure the regex does not capture ports (:1234) by using \b (word boundaries).





---

Usage in Code

JavaScript Example

JavaScript:
const ipv4Regex = /\b((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\b/;



const testCases = [

  "192.168.1.1", // Valid

  "10.0.0.1", // Valid

  "255.255.255.255",// Valid but not unicast

  "127.0.0.1", // Loopback

  "224.0.0.1", // Multicast

  "192.168.1.1:8080", // Shouldn't match due to port

];



testCases.forEach((ip) => {

  console.log(`${ip}: ${ipv4Regex.test(ip)}`);

});





---

Strict Unicast Validation

If you want to strictly enforce unicast IPv4 ranges, add an additional programmatic check after matching:

1. Ensure the first octet is between 1 and 223.


2. Exclude 127.x.x.x.



JavaScript Example with Unicast Check

JavaScript:
function isUnicastIPv4(ip) {

  const ipv4Regex = /\b((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\b/;



  if (!ipv4Regex.test(ip)) return false;



  const [firstOctet] = ip.split(".");

  const firstOctetNum = parseInt(firstOctet, 10);



  return firstOctetNum >= 1 && firstOctetNum <= 223 && firstOctetNum !== 127;

}



console.log(isUnicastIPv4("192.168.1.1")); // true

console.log(isUnicastIPv4("127.0.0.1")); // false

console.log(isUnicastIPv4("224.0.0.1")); // false

console.log(isUnicastIPv4("255.255.255.255")); // false

console.log(isUnicastIPv4("10.0.0.1")); // true





---

Conclusion

Use the regex to match valid IPv4 addresses.

Add programmatic logic for stricter rules like unicast validation.

The
regex avoids ports by using \b and does not capture strings like 192.168.1.1:8080.
 
Back
Top