const regex = new RegExp('\\d: ([\\w@]+): <(.+)> mtu (\\d+) qdisc (\\w+) qlen (\\d+)\\n[ ]+(\\w+\\/\\w+) ([\\d:\\w.]+) brd ([\\d:\\w.]+)\\n?(?:(?: )+inet6? ([\\d.:\\/]+) (?:brd ([\\d:\\w.]+) )?scope (\\w+) (?:\\w+)?\\n(?: )+valid_lft (\\w+) preferred_lft (\\w+))?', 'gm')
const str = `1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: lan1: <BROADCAST,MULTICAST,UP,LOWER_UP8000> mtu 1500 qdisc mq qlen 1000
link/ether 00:60:e0:6a:48:26 brd ff:ff:ff:ff:ff:ff
inet 10.32.39.55/24 brd 10.32.39.255 scope global lan1
valid_lft forever preferred_lft forever
3: lan2: <BROADCAST,MULTICAST,UP,LOWER_UP8000> mtu 1500 qdisc mq qlen 1000
link/ether 00:60:e0:6a:48:27 brd ff:ff:ff:ff:ff:ff
inet 192.168.90.1/20 brd 192.168.95.255 scope global lan2
valid_lft forever preferred_lft forever
4: sit0@NONE: <NOARP> mtu 1480 qdisc noop qlen 1
link/sit 0.0.0.0 brd 0.0.0.0
6: wwp0s29u1u3i12: <BROADCAST,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:00:11:12:13:14 brd ff:ff:ff:ff:ff:ff
inet 172.16.224.124/16 brd 172.16.255.255 scope global wwp0s29u1u3i12
valid_lft forever preferred_lft forever
`;
// 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