const regex = /^(?!.*::.*::)fe80(?=(?::+[^:]+){0,6}$)(?:(:0{1,4})*|(?1){3,}(?::+[^:]+)):(?=:)(:[0-9a-f]{1,4}){0,4}$|^fe80(?=(?::[^:]+){7}$)(?1){3,}(?2){0,4}$/gmi;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?!.*::.*::)fe80(?=(?::+[^:]+){0,6}$)(?:(:0{1,4})*|(?1){3,}(?::+[^:]+)):(?=:)(:[0-9a-f]{1,4}){0,4}$|^fe80(?=(?::[^:]+){7}$)(?1){3,}(?2){0,4}$', 'gmi')
const str = `valid cases:
FE80::1
FE80::1:0:1
FE80::0000:1
FE80:0::1
FE80:000::1
FE80:0:00::1
FE80:0:00:000::1
FE80:0:00:000:0000::1
fe80::9656:d028:8652:66b6
fe80:0000:0000:0000:0000:0000:0000:0000
fe80:00:000:0000:1234:5678:ABCD:EF
fe80:00:000:0000:1234:5678:ABCD:00
fe80::1234:5678:ABCD:EF
fe80:00:000:0000::EF
fe80:00::ABCD:EF
FE80:0:00:000:0001::1
FE80:0:00:000:0000:1::1
invalid cases:
fe80:66b6:9656:d028:8652:66b6:9656:d028:0000
fe80:66b6:9656:d028:8652:66b6:9656:d028
fe80:1:000:0000:1234:5678:ABCD:EF:9999
fe80::bad:1234:5678:ABCD:EF
fe80:0:bad::EF
fe80:1:2:3:4:6:5:7
fe80:0:bad:EF
Fe90::1
2000::1
fe80:00:000:0000:0:0:0::EF
fe80:0:0:0:ABCD:EF:0
FE80:0:00:001:0001::1
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions