Skip to content

Instantly share code, notes, and snippets.

@kLiHz
Last active February 15, 2025 13:40
Show Gist options
  • Save kLiHz/8a48f4927301f37d299afeaa98ac5532 to your computer and use it in GitHub Desktop.
Save kLiHz/8a48f4927301f37d299afeaa98ac5532 to your computer and use it in GitHub Desktop.
JavaScript RegExp to match valid IPv4 / IPv6 address
const d8 = '25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d';
export const ipv4RegStr = `((${d8})\\.){3}(${d8})`
export const ipv4Matcher = new RegExp(ipv4RegStr, 'g');
export const ipv4Validator = new RegExp(`^(${ipv4RegStr})$`);
// https://stackoverflow.com/questions/14638711/regular-expression-for-ipv6-addresses
const h16 = '[0-9a-fA-F]{1,4}';
const ls32 = `((${h16}:${h16})|(${ipv4RegStr}))`;
export const ipv6RegStr = [
`(${h16}:){6}${ls32}`,
`::(${h16}:){5}${ls32}`,
`(${h16})?::(${h16}:){4}${ls32}`,
`((${h16}:){0,1}${h16})?::(${h16}:){3}${ls32}`,
`((${h16}:){0,2}${h16})?::(${h16}:){2}${ls32}`,
`((${h16}:){0,3}${h16})?::(${h16}:){1}${ls32}`,
`((${h16}:){0,4}${h16})?::${ls32}`,
`((${h16}:){0,5}${h16})?::${h16}`,
`((${h16}:){0,6}${h16})?::`,
].join('|')
// Can be used as either mather or validator
export const ipv6Matcher = new RegExp(ipv6RegStr, 'g');
export const ipv6Validator = new RegExp(`^(${ipv6RegStr})$`);
export const ipv6RegStr_1 = [
`(${h16}:){7}(:|(${h16}))`,
`(${h16}:){6}((:(${h16})?)|(${ipv4RegStr}))`,
`(${h16}:){5}:(((${h16})(:${h16})?)|(${ipv4RegStr}))`,
`(${h16}:){4}(:|(:${h16}){1,3}|(:(${h16}:){0,1}${ipv4RegStr}))`,
`(${h16}:){3}(:|(:${h16}){1,4}|(:(${h16}:){0,2}${ipv4RegStr}))`,
`(${h16}:){2}(:|(:${h16}){1,5}|(:(${h16}:){0,3}${ipv4RegStr}))`,
`(${h16}:){1}(:|(:${h16}){1,6}|(:(${h16}:){0,4}${ipv4RegStr}))`,
`:(:|(:${h16}){1,7}|(:(${h16}:){0,5}${ipv4RegStr}))`,
].join('|')
// Using "lookarounds" to enforce a complete match. https://stackoverflow.com/a/32368136
export const ipv6Matcher_1 = new RegExp(`(?:^|(?<=[^.:a-fA-F0-9]))(${ipv6RegStr_1})(?=[^.:a-fA-F0-9]|$)`, 'g')
export const ipv6Validator_1 = new RegExp(`^(${ipv6RegStr_1})$`);
import { ipv6Validator, ipv6Validator_1 } from './IPRegExp.ts'
const validIPv6Addresses = [
'2001::',
'2001:db8::',
'::',
'::1',
'::456:789',
'2001:db8::1',
'2001:db8::456:789',
'2001:db8:0:0:1:2:3:4',
'2001:db8:0::1:2:3:4',
'::127.0.0.1',
'::ff:127.0.0.1',
'2001::127.0.0.1',
'2001:db8::127.0.0.1',
'2001:db8:1:2:0:0:127.0.0.1',
'2001:db8:1:2:0::127.0.0.1',
'2001:db8:1:2::127.0.0.1',
]
const invalidIPv6Addresses = [
'',
'2001:',
':2001:',
':2001',
'2001:db8:::456:789',
'2001:db8::456:789:',
'2001:db8::456::789',
'2001:db8::456::789:',
'2001:db8:0::1:2:3:4:5',
'2001:db8:1:2:0:0:0:127.0.0.1',
'2001:db8:1:2:0:0::127.0.0.1',
'1:2:3:4:1:2:3:4:',
':1:2:3:4:1:2:3:4:',
':1:2:3:4:1:2:3:4',
'1:2:3:4:1:2:3:4:5',
':1:2:3:4:1:2:3:4:5:',
'::127.0.1',
]
console.log('Valid IPv6 address should pass the test.');
console.log(validIPv6Addresses.map(i => [ipv6Validator.test(i), ipv6Validator_1.test(i), i]));
console.log('Invalid IPv6 address should fail the test (returning false).');
console.log(invalidIPv6Addresses.map(i => [ipv6Validator.test(i), ipv6Validator_1.test(i), i]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment