Skip to content

Instantly share code, notes, and snippets.

@keepitsimple
Last active August 8, 2018 14:40
Show Gist options
  • Save keepitsimple/70fbc875548e2dd5aaa5ca280e708a10 to your computer and use it in GitHub Desktop.
Save keepitsimple/70fbc875548e2dd5aaa5ca280e708a10 to your computer and use it in GitHub Desktop.
JavaScript the best RegEx for IPv4 validation with test examples
const ip4RegEx = /^(?:(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])(\.(?!$)|$)){4}$/
/*
//valid
127.0.0.1
192.168.1.1
192.168.1.255
255.255.255.255
0.0.0.0
1.1.1.1
//invalid
01.1.1.1
1.01.1.1
1.1.01.1
1.1.1.01
192.168.1.1.
192.168.1.1.1
30.168.1.255.1
127.1
192.168.1.256
-1.2.3.4
3...3
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment