const regex = /(?:^|\s)(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|2[0-4]\d|1\d{0,2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{0,2}|[1-9]?\d)|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|2[0-4]\d|1\d{0,2}|[1-9]?\d)\.){3}(25[0-5]|2[0-4]\d|1\d{0,2}|[1-9]?\d))(?=\s|$)
/igm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?:^|\\s)(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|2[0-4]\\d|1\\d{0,2}|[1-9]?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{0,2}|[1-9]?\\d)|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|2[0-4]\\d|1\\d{0,2}|[1-9]?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{0,2}|[1-9]?\\d))(?=\\s|$)
', 'igm')
const str = `{1: Initial address, regex should say valid/match}
2001:0db8:0000:0000:0000:ff00:0042:8329
{2: After removing all leading zeroes, regex should say valid/match}
2001:db8:0:0:0:ff00:42:8329
{3: After omitting consecutive sections of zeroes, regex should say valid/match}
2001:db8::ff00:42:8329
{4: The loopback address, regex should say valid/match}
0000:0000:0000:0000:0000:0000:0000:0001
{5: The loopback address be abbreviated to ::1 by using both rules, regex should say valid/match}
::1
{6: This should be valid, regex should say valid/match}
ABCD:ABCD:ABCD:ABCD:ABCD:ABCD:192.168.158.190
{7: This should NOT be valid/match}
::
{These two formats allows IPv6 applications to communicate directly with IPv4 applications, regex should say valid/match}
{8}
0:0:0:0:0:ffff:192.1.56.10
{9}
::ffff:192.1.56.10/96
{These next two formats are used for tunneling. It allows IPv6 nodes to communicate across an IPv4 infrastructure, regex should say valid/match}
{10}
0:0:0:0:0:0:192.1.56.10
{11}
::192.1.56.10/96
{These 4 should be valid/match}
{12}
::FFFF:129.144.52.38
{13}
::129.144.52.38
{14}
::FFFF:d
{15}
1080:0:0:0:8:800:200C:417A
{These 4 should NOT be valid/match}
{16}
::FFFF:d.d.d
{17}
::FFFF:d.d
{18}
::d.d.d
{19}
::d.d`;
// 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